I'm trying to create a database notifcation in Laravel 5.7 and getting this SQL QueryException. My $notifiable variable in the toArray method is the user and this id in the exception: invalid input syntax for integer: \"10337e35-8da9-4600-b7de-792398eb6f48\"
is the correct id for the user to be notified. I have my notifications table set up in the standard way:
Schema::create('notifications', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('type');
$table->morphs('notifiable');
$table->text('data');
$table->timestamp('read_at')->nullable();
$table->timestamps();
});
My user table is set up like this:
Schema::create('users', function (Blueprint $table) {
$table->uuid('id');
$table->primary('id');
$table->string('name');
$table->string('username')->unique();
$table->string('email');
$table->string('password');
$table->rememberToken();
$table->timestamps();
$table->softDeletes();
});
It is set up with a uuid as the primary id. Beyond that, the notification itself has database
in the via method:
public function via($notifiable)
{
return ['database', 'mail'];
}
I've set a couple properties in the toArray method. Nothing complicated. I also tried to manually insert into the notifications table and I can insert an entry with a notifiable_id
as an int, but not a string/uuid. It's mind boggling to me that the nofiable system would not be set up to allow for uuid's as ids. Maybe it's just a small tweak, but I'm not seeing it out there and figure surely someone else has run into this. Cheers!
Full exception:
Object { message: "SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for integer: \"0e7cc3d8-bfca-41c1-99f8-6da9e8887465\" (SQL: insert into \"notifications\" (\"id\", \"type\", \"data\", \"read_at\", \"notifiable_id\", \"notifiable_type\", \"updated_at\", \"created_at\") values (f6206d4e-c98f-4ced-a1ee-6a755f073807, App\\Notifications\\BugTransition, {\"bug_id\":\"be14d750-ba6c-4e70-82f7-a36786ff37f4\",\"workflow_state\":\"accepted\"}, , 0e7cc3d8-bfca-41c1-99f8-6da9e8887465, App\\User, 2018-10-22 22:35:54, 2018-10-22 22:35:54))", exception: "Illuminate\\Database\\QueryException", file: "/srv/app/vendor/laravel/framework/src/Illuminate/Database/Connection.php", line: 664,
The issue is notifiable_id
. $table->morphs('notifiable');
creates an integer column, but you are trying to store a string.
You have to create the columns (and the index) yourself:
$table->string('notifiable_type');
$table->string('notifiable_id');
$table->index(['notifiable_type', 'notifiable_id']);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With