【发布时间】:2019-03-27 01:32:50
【问题描述】:
我正在尝试在 Laravel 5.7 中创建数据库通知并获取此 SQL QueryException。我在 toArray 方法中的 $notifiable 变量是用户,并且异常中的这个 id:invalid input syntax for integer: \"10337e35-8da9-4600-b7de-792398eb6f48\" 是要通知的用户的正确 id。我以标准方式设置了通知表:
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();
});
我的用户表是这样设置的:
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();
});
它设置了一个 uuid 作为主 ID。除此之外,通知本身在 via 方法中有database:
public function via($notifiable)
{
return ['database', 'mail'];
}
我在 toArray 方法中设置了几个属性。没什么复杂的。我还尝试手动插入到通知表中,我可以插入带有notifiable_id 作为int 的条目,但不能插入字符串/uuid。令我难以置信的是,nofiable 系统不会设置为允许 uuid 作为 id。也许这只是一个小调整,但我没有在外面看到它,并且肯定有人遇到过这个问题。干杯!
完全例外:
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,
【问题讨论】:
-
你有没有在你的模型中设置
public $incrementing = false; protected $keyType = 'string'; -
没有。那只是用户模型吗?
-
更正,我确实设置了
public $incrementing = false;,但没有设置keyType。 -
在这两个模型中(以及所有其他具有字符串键的模型)。
-
那没有效果。我需要创建一个雄辩的通知模型来设置它们吗?
标签: php laravel laravel-5 notifications laravel-5.7