How to get last inserted id through save() method in laravel

后端 未结 6 1716
醉酒成梦
醉酒成梦 2021-01-22 03:37

I know to get the last id I can use insertGetId() but I want to know how can I get the last inserted id through save() method.

order =          


        
6条回答
  •  后悔当初
    2021-01-22 04:10

    You can get it by like below :

    $order = new Store_order;
    $order->invoice_id = $signed['invoice_id'];
    $invoice = $order->save();
    echo $invoice->so_id;
    

    in this case you no need to store in one variable and then access it, You can get the inserted records by calling the model object itself :

        $order = new Store_order;
        $order->invoice_id = $signed['invoice_id'];
        $order->save();
        // echo $order; => will return entire stored last record.
        echo $order->so_id;
    

    Make sure so_id is autoincrement.

提交回复
热议问题