You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel执行ArticlesSeeder填充报Array to string conversion错误

报错原因

该报错和$table->text('body')字段配置无关,text类型完全适配长文本正文存储,你编写的body字段赋值内容是合法字符串,不会触发转换错误。
报错的根源在ArticlesSeeder.php的待插入数据定义部分:你给updated_at、deleted_at两个字段赋值时,错误地将NULL包裹在了数组中写成[NULL],传入的是数组类型值,但数据库字段期望接收时间字符串或NULL标量值,Laravel执行插入操作尝试将数组转为字符串时,就触发了Array to string conversion错误。

修复方法

直接修改ArticlesSeeder.php中对应字段的赋值,去掉包裹NULL的方括号即可,修改后的待插入数组片段如下:

$articles[] = [
    "slug"     => $slug = "article_$index",
    "catagory" => "category_$index",
    "title"    => "Title of $slug",
    "author"   => "$slug@remissiondigital.com",
    "body"    => "This isn't the \"real\" body of the $slug article\r\n
                    Here is more body\r\n
                    And more here too\r\n\r\n
                    just a seeder\r\n
                    more text",
    "created_at" => Carbon::now(),
    "updated_at" => NULL,
    "deleted_at" => NULL,
];

修改完成后重新执行php artisan db:seed --class=ArticlesSeeder命令即可正常完成填充。

补充说明:你的迁移文件中timestampsTz()、softDeletesTz()方法生成的字段本身就允许传入NULL值,直接传入标量NULL完全符合字段定义要求,不会产生类型兼容问题。

内容的提问来源于stack exchange,提问作者Tariq

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.03 03:21:45