Laravel - 在日期之间为每个日期添加行
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
数据库中 - 行,foreach 日期介于“from
”和“to
”之间,而这一行只是为了具有 auction_id = $auction->id;
和price = $auction->price
...其他字段为空...
我该怎么做?那么如何在 from
和 to
之间设置 foreach 日期,并根据请求存储拍卖 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();
}
这里我有将我的 $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
数据库中 - 行,foreach 日期介于“from
”和“to
”之间,而这一行只是为了具有 auction_id = $auction->id;
和price = $auction->price
...其他字段为空...
我该怎么做?那么如何在 from
和 to
之间设置 foreach 日期,并根据请求存储拍卖 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();
}