【问题标题】:Laravel - add row foreach date between datesLaravel - 在日期之间添加行 foreach 日期
【发布时间】:2016-08-15 23:34:42
【问题描述】:

这里有将我的$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 ... 其他字段空……

我该怎么做?那么如何在fromto 之间设置foreach 日期,并根据请求存储拍卖ID 和价格...

【问题讨论】:

标签: php laravel foreach eloquent store


【解决方案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();
    }

【讨论】:

    猜你喜欢
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-09
    相关资源
    最近更新 更多