Solve MySQL Data Truncated Error Message in Laravel Notifications

Mona Moxie
Coding Tips and Tutorials
3 min readNov 28, 2019

--

Usually, when creating database notifications with Laravel, you may come across cases where you want a copy of these notifications to be stored and later queried from your database.

As you already know, the migration file will be auto-generated with the command:

php artisan notifications:table

If you run the above command, you should expect to get a new migration with some lines similar to the snapshot below

Before I show you where the issue comes up, run the migrate command and let’s see the structure of this table that has just been created.

php artisan migrate

Fire up PHPMyAdmin or any other tool you use and check it out.

If you scroll back to the migration file, you’ll notice there was no “notifiable_type” and “notifiable_id” in it, there was only “notifiable” with a datatype of “morphs”.

Now take note of the data type of the notifiable_id within that file. It’s probably using a bigInt datatype with a length of 20.

This should work for most cases where your notifiable_id (ID of object to be notified) is of the data type bigInt or any thing similar, with a length of not more than 20 characters.

But what happens when the data type you are using for that model is a Uuid? In that case, you’ll get the error below.

By default, Laravel Notifications is designed to use an int data type to store the user_id.

Now if your application uses a different data type to identify users (for example: UUIDs), you should modify the default migration file after generating.

To solve this:

Generate the migration as usual but don’t run the migrate command. Simply generate and edit the newly created migration.

Change from this:

To this:

Then simply run your migrate command, and there you have it.

Works the same.

--

--