【问题标题】:Laravel: Create a Table and Update Another Table simultaneously in a ControllerLaravel:在控制器中同时创建一个表并更新另一个表
【发布时间】:2020-09-21 23:30:10
【问题描述】:

我正在尝试在 Transactions 表中创建一条新记录。同时尝试更新另一个表。

这就是我所做的。


    public function store(TransactionRequest $request, Item $item)
    {
        //this part works fine
        Transaction::create([
            'patron_id' => $request->patron_id,
            'item_id' => $request->item_id,
            'loaned' => $request->loaned,
            'due' => $request->due,
        ]);

        //THIS PART IS NOT WORKING
        Item::find($request->item_id);
        $item->update([
            'onloan' => "1",
        ]);

        return redirect(route('transactions.index'));

    }

请帮忙。谢谢。

【问题讨论】:

标签: laravel


【解决方案1】:

你可以试试这个

 Item::find($request->item_id)
->update([
            'onloan' => "1",
        ]);

【讨论】:

    【解决方案2】:

    这应该适合你:

    Item::find($request->item_id)->update(['onloan' => "1"]);
    

    或者,你应该写这样的代码:

        $item = Item::find($request->item_id);
        $item->update([
            'onloan' => "1",
        ]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-26
      • 2020-12-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多