【问题标题】:Laravel - how to save multiple rowsLaravel - 如何保存多行
【发布时间】:2016-08-17 11:12:02
【问题描述】:

我有这个存储方法:

    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 的每一天的空记录...只需将已保存拍卖的 id 放在 offer 表中的 auction_id 中...

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

【问题讨论】:

    标签: php laravel for-loop request store


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

    【讨论】:

    • AUTH 在哪里?还如何制作循环以在 $start 和 $end 之间的每一天保存行
    • 我正在编写用于编写多行的代码,请稍等一下,关于 \Auth: 是 user_id 引用了拍卖用户吗?
    • 使用 \Auth::user() 将 user_id 添加到当前登录用户的拍卖中,就像你拥有它一样,关于循环,你要插入 91 行吗?分别与日期?
    猜你喜欢
    • 1970-01-01
    • 2017-11-20
    • 2022-01-14
    • 2017-08-14
    • 2021-12-31
    • 2013-08-17
    • 2019-02-01
    • 1970-01-01
    • 2016-12-03
    相关资源
    最近更新 更多