Laravel - 在日期之间添加行foreach日期

时间:2016-04-22 11:03:50

标签: php laravel foreach eloquent store

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

public function store(Requests\OfferRequest $request)
    {

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

            Auth::user()->auctions()->save($auction);
}

现在我从请求获取$ from和$ to fields:

$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 ...其他字段为空...

我怎么能这样做?那么如何让foreach日期在fromto之间,以及拍卖的商店ID和来自请求的价格......

1 个答案:

答案 0 :(得分:1)

这很简单。

$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();
    }
相关问题