Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Abrahamghaemi's avatar

generate uuid for Model (without migration, id column is uuid)

how to generate uuid for User model or other model id?

  • notice: i haven't laravel migration my backend is other platform and id default is uuid.

App\Models\User

    protected $casts = [
        'email_verified_at' => 'datetime',
        'id' => 'string'
    ];

App\Http\Controllers\Auth\RegisterController

        return User::create([
            'id' => uniqid() . substr(md5(rand()), 0, 4),
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);

0 likes
6 replies
Abrahamghaemi's avatar

exception occurs

General error: 1364 Field 'id' doesn't have a default value

SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value (SQL: insert into `user_frontend` (`name`, `email`, `password`, `updated_at`, `created_at`) values ("rafael", [email protected], y$PDbOvL5FUTA4S8xgvRTK.e2VrQD2pscvWLsGqKD.343, 2021-02-27 03:55:44, 2021-02-27 03:55:44))
Abrahamghaemi's avatar

yes man, id was protected. Updated to fillable that works. There was another point: the user ID column was varchar 24 and Str generates more than 24 characters and get exception, i roll back to own pattern and work.

thank you so much

[
id => uniqid() . substr(md5(rand()), 0, 4),
]

1 like
Abrahamghaemi's avatar

Incrementing set to false. casts property id => string that confuses me when id default autoincrement. now registration safe work.

    protected $casts = [
        'email_verified_at' => 'datetime',
        'id' => 'string'
    ];

    public $incrementing = false;

Please or to participate in this conversation.