【问题标题】:how to retrieve the id just created in the database in laravel? [duplicate]如何在laravel中检索刚刚在数据库中创建的id? [复制]
【发布时间】:2020-03-24 19:27:24
【问题描述】:

如何检索刚刚在数据库中创建的 id,当我按下保存按钮时,数据已创建,我想从该数据中检索 id

这是我的控制器代码

$cart= new cart;
        $cart->user_id = $request->user_id;
        $cart>vendor_name = $request->vendor_name;
$cart->save();

我要检索刚刚创建的数据的id

【问题讨论】:

    标签: laravel laravel-5 eloquent


    【解决方案1】:

    保存后,您可以像这样访问 id:

    $cart->id
    

    这将是您最近创建的卡片的 ID。

    【讨论】:

      【解决方案2】:
      $cart= new cart;
              $cart->user_id = $request->user_id;
              $cart>vendor_name = $request->vendor_name;
      $cart->save();
      
      $cart->id; //here you still have access to the data and you can access the id like this.
      

      user_id 和任何其他字段也是如此,您可以像这样访问它们:$cart-> user_id;

      【讨论】:

        【解决方案3】:

        您可以通过此 $cart 访问所有信息。保存后您只需

        $cart->id (to get id)
        $cart->user_id(to get user_id)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-07-18
          • 2011-06-21
          • 2011-09-09
          • 2013-11-14
          • 1970-01-01
          • 2013-05-03
          相关资源
          最近更新 更多