Search code examples
phplaravelforeacheloquentstore

Laravel - add row foreach date between dates


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

public function store(Requests\OfferRequest $request)
    {

            $auction= new Auction($request->all());

            Auth::user()->auctions()->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...


Solution

  • It is quite simple.

    $start_date = \Carbon\Carbon::parse($auction->from);
    $end_date = \Carbon\Carbon::parse($auction->to);
    
    
        while(!$start_date->eq($end_date))
        {
            $temp = new maxoffers;
            $temp->id = $auction->id;
            $temp->price = $auction->price;
    
            //$temp->something_else = 'value';
            //If Your structure does not support null values then you must define some values here.
    
            $temp->save()
    
            $start_date->addDay();
        }