Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

aleksov's avatar

Eloquent - create rows foreach date

Here I have code that store my $auction into auction database:


public function store(Requests\OfferRequest $request)
    {

            $auction= new Auction($request->all());
           
            Auth::user()->auction()->save($auction);
}

Now I from request get $from and $to fields:


$auction->from //return me start date for auction
$auction->to // return me end date for auction

Now I want to store into maxoffers database - rows, foreach date between 'from' and 'to' and this rows just to have auction_id = $auction->id; and price = $auction->price ... other fields to be empty...

How I can do that? So how to make foreach date between from and to and store id of auction and price from request...

0 likes
7 replies
RoboRobok's avatar

I'm not sure I follow. Two things:

  1. Isn't your relationship method supposed to be called auctions(), not auction()?
  2. If I understand your question: if start is April 19th and end if April 22nd, you want to insert records with April 19th, April 20th, April 21st and April 22nd? Why don't you just insert all bids with dates and then retrieve them with something like GROUP BY DATE(placed_at) ORDER BY placed_at? And then filling the missing dates, which will be easy.
aleksov's avatar
  1. yes auctions() ...

  2. Yes I want to instert record for 19. 20. 21. 22. April. explanation: I build auction for dates (so each date is one auction)... Now when someone place bid then I will check in database is bid for that day higher than current bid and update that row. maxoffers is actually current winners. So I need an row for each day - for winner. How to create it?

RoboRobok's avatar

Still, I don't see why you want to keep days in the database. It's data redundancy, unless you do some really complex stuff with each day. If not, just keep start and end dates with an auction, but keep bids with their dates only. It's easy enough to retrieve highest bid for particular date, there's no need to store it.

aleksov's avatar

ok, show me how? at max offers I have etc. offer for 19. apr. and 22. apr. How to fill missing data (20,21 apr. ) ? So how to write function with return me all bids + missing dates ...

aleksov's avatar

Ok here I have function which return me all bids from maxoffers: public function maxoffers($id) { $start = '04-04-2016'; $end = '05-05-2016'; $offers = Maxoffer::where('article_id', $id)->latest()->get(['id', 'price', 'start', 'user_id']);

});

How to get offers with all dates...

aleksov's avatar

Do I need to use foreach or what? How to create rows from start($auction->from) to end($auction->to)

Please or to participate in this conversation.