Laravel - 如何保存多行

时间:2016-04-23 22:35:29

标签: php laravel for-loop request store

我有这种商店方法:

    public function store(Requests\OfferRequest $request)
        {
    $start = $request->input('from'); // 04/24/2016 12:00 AM
    $end = $request->input('to); // 07/24/2016 12:00 AM

                $auction = new Auction($request->all());
                Auth::user()->auctions()->save($auction);
//how to get saved auction ID ?
    }

现在我需要在Offer表中保存每天从$ start到$ end的空记录...只需要在auction_id中将已保存拍卖的id放在offer表中...

我可以将其保存在一个查询中,还是需要使用for循环?什么是最好的方式?

1 个答案:

答案 0 :(得分:1)

改为使用这种方式:

//how to get saved auction ID ?
$auction = new Auction();
$auction->fill($request->all());
$auction->user_id = \Auth::user()->id;
$auction->save();
$auction_id = $auction->id;

$dStart = new DateTime($start);
$dEnd   = new DateTime($to);

// Will loop through everyday from $dStart to $dEnd
while ($dStart < $dEnd) {        
    // current date of the loop formated for db use
    $date = $dStart->format('Y-m-d');

    // Code to insert date to Offer 


    $dStart->add(new DateInterval("P1D"));
}