【问题标题】:Laravel 5.7 - Invalid text representation: 7 ERROR: invalid input syntax for integer: \"10337e35-8da9-4600-b7de-792398eb6f48\"Laravel 5.7 - 无效的文本表示:7 错误:整数的无效输入语法:\“10337e35-8da9-4600-b7de-792398eb6f48\”
【发布时间】: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


【解决方案1】:

问题是notifiable_id$table->morphs('notifiable'); 创建了一个整数列,但您正在尝试存储一个字符串。

您必须自己创建列(和索引):

$table->string('notifiable_type');
$table->string('notifiable_id');
$table->index(['notifiable_type', 'notifiable_id']);

【讨论】:

  • 您想用 uuid 存储 notifiable_type 时都是这种情况?有点傻哈。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
  • 2021-09-21
  • 2020-10-01
  • 2014-07-17
  • 2020-04-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多