[英]Laravel-5 get last inserted id after firstOrCreate()
我試圖在firstOrCreate()調用后獲取數據庫中最后插入的ID。 我復制了下面的代碼:
$booking->firstOrCreate(array_slice($reserve->whereId($payment->reserved_place_id)->first()->toArray(), 0, 24))->save();
如何插入最后一個ID?
我查看了其他Laravel請求獲取最后一個插入ID但我相信這個實例是不同的,因為他們沒有使用firstOrCreate()。
提前致謝。
firstOrCreate()
方法返回Model。 因此,您只需將其保存到某個變量,然后獲取模型的ID即可。
// Retrieve the flight by the attributes, or create it if it doesn't exist...
$flight = App\Flight::firstOrCreate(['name' => 'Flight 10']);
返回Laravel文檔中所述的模型:
您可以使用另外兩種方法通過批量分配屬性來創建模型:firstOrCreate和firstOrNew。 firstOrCreate方法將嘗試使用給定的列/值對定位數據庫記錄。 如果在數據庫中找不到該模型,則將插入具有給定屬性的記錄。
$flight->id; //returns the last inserted id or the id of the already created document
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.