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

mickiy1's avatar

Laravel Notification Table

Hello, everyone, I am working on saving notifications in the notification table which is working fine but the primary key of the tables are being generated randomly instead of auto-incrementing, how can I fix it

    {
        Schema::create(
            'notifications', function (Blueprint $table){
            $table->bigIncrements('id');
            $table->bigInteger('user_id');
            $table->text('type', 30);
            $table->text('data');
            $table->tinyInteger('is_read');
            $table->timestamps();
        }
        );
    }``` This is the migration file of the table
0 likes
14 replies
mickiy1's avatar

if i save one notification the primary key is 1, if i save another now the PK will be 45, the next 35467, the next 466576...

Sinnbeck's avatar

@mickiy1 Ok that sounds very very strange. If you use debugbar, can you see the query being written and where from ?

mickiy1's avatar

@sinnbeck, one use case I am using this for is notifying users when they are assigned to be a project manager what I did was in the controller that is used to assign users as PM, i added this code

$user = User::find($request->project_manager); $user->notify(new ProjectManagerAssignNotification($project));

so it can be saved in the notification table

Sinnbeck's avatar

I dont see any reason why that would break the auto increment. It seems that it is setting the ID manually, but I wonder what part of laravel would do that. Debugbar should show that

AddWebContribution's avatar

Please try this for auto-increment in laravel migration

Schema::create( 'notifications', function (Blueprint $table){ $table->increments('id'); } );

Sinnbeck's avatar

@saurabhd Any reason that it would work with ->increments() and not ->bigIncrements() ? They are identical, apart from INT vs BIGINT

mickiy1's avatar

Thank you, everyone for the help, i ended up using uuid

jeanne's avatar

I had the same issue! The problem is (I think) the following: Laravels default notifications setup uses a UUID instead of an incrementing integer for the primary key of the Notifications table. You're fine to change that as you did, but you also have to tell Laravel to generate an integer instead of a string. Ohteriwse it probably takes the first integers in the UUID it generated and fill that, causding the random id values. You can do this in your notifications model. Since your model extends DatabaseNotification, you need to override the settings on DatabaseNotification. Set these two values in your Notification model. It fixed the problem for me!

class Notification extends DatabaseNotification
{
    protected $keyType = 'integer';

    public $incrementing = true;

Please or to participate in this conversation.