Laravel - 如何保存多行
Laravel - how to save multiple rows
我有这个存储方法:
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 ?
}
现在我需要在报价 table 中保存从 $start 到 $end 的每一天的空记录...只需要在报价 auction_id 中保存已保存拍卖的 ID table...
我可以将其保存在一个查询中还是需要使用 for 循环?什么是最好的方法?
改用这种方式:
//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"));
}
我有这个存储方法:
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 ?
}
现在我需要在报价 table 中保存从 $start 到 $end 的每一天的空记录...只需要在报价 auction_id 中保存已保存拍卖的 ID table...
我可以将其保存在一个查询中还是需要使用 for 循环?什么是最好的方法?
改用这种方式:
//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"));
}