Laravel-5在firstOrCreate()之后获取最后插入的id


Laravel-5 get last inserted id after firstOrCreate()

我正试图在第一次OrCreate()调用后获得数据库中最后一个插入的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