webpatser/laravel-uuid

Get Last Inserted ID in a Model?

icangku opened this issue · 4 comments

How to get last inserted id ?

I had the same question @webpatser :)

I've tried the standard method of requesting $model->id after a $model->save(), which returns 0 and I've tried setting the UUID as a variable $uuid = Uuid::generate(4)->string; but the UUID saved to the database table is different to the $uuid variable.

I found the answer.It was thought that there were three places in the Model::save method to determine whether it was self-increasing or not.

protected $primaryKey = 'uuid';
protected $keyType = 'string';
// Must be set Because Model::save uses this to determine whether it's self-increasing or not.
public $incrementing = false;

It does work! Thanks man!

work fine !