michaelpittino's avatar

Generate unique string as id for models automatically

Hello guys! I'm not a fan of $table->increments so I use unique strings as id. The problem is I'd have to make id fillable so I can assign the id when I create a database entry. But I don't want that. Is there a way to automatically generate a id for a model before it gets inserted in the database? I think of something like a global hook or global event:

beforeModelInsertDb($model) {
    $model->id = generateUniqueId();
}

where beforeModelInsertDb gets called by laravel automatically.

Is there any way to do this?

0 likes
5 replies

Please or to participate in this conversation.