拉拉维尔 - 为日期之间的每个日期添加行


Laravel - add row foreach date between dates

在这里,我有将我的$auction存储到拍卖数据库中的代码:

public function store(Requests'OfferRequest $request)
    {
            $auction= new Auction($request->all());
            Auth::user()->auctions()->save($auction);
}

现在我从请求中获取$from和$to字段:

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

现在我想存储到maxoffers数据库中 - 行,用于"from"和"to"之间的每个日期,而这些行只是为了auction_id = $auction->id;price = $auction->price......其他字段为空...

我该怎么做?那么如何在fromto之间制作每个日期,并从请求中存储拍卖的ID和价格......

这很简单。

$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();
    }