Eloquent / Laravel: How to get the last insert / update id / instance updateOrCreate ()?

The name is mostly self-evident. Eloquent has a method called

updateOrCreate()

here: https://laravel.com/docs/5.5/eloquent#other-creation-methods

In some cases, this is really helpful. However, after execution, updateOrCreate()I need either an updated / created object, or its primary key or its identifier.

Of course, I could do MyModel::where(...)->first()and provide all this data again, but it is inconvenient and can be expensive.

However, updateOrCreate()returns true or false .

Any ideas?

+4
source share
2 answers

, :

$object = Model::updateOrCreate(['name' => 'John'], ['age' => 25]);

$id = $object->id;
+6

$lastRecord = MyModel::last();

$lastRecord = MyModel::orderBy('id', 'DESC')->first();
-1

Source: https://habr.com/ru/post/1688168/


All Articles