我正在尝试运行一个播种器类:
insert( ['name' => 'PhD'], ['name' => 'Master'], ['name' => 'Bachelor'], ['name' => 'Foundation'], ['name' => 'ESL'] ); } }
我执行以下命令:
art db:seed --class CourseTableSeeder
我希望看到数据库中的五个值,但是,我只看到第一个“ phd”。
我尝试通过->toSql()
在插入程序上运行来调试它,但我认为这是不可能的,并且由于插入程序最有可能返回布尔值,因此无法链接,因此我收到以下错误
[2018-07-23 15:35:45] local.ERROR: Call to a member function toSql() on boolean {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Call to a member function toSql() on boolean at C:\\laragon\\www\
eports \ database \ seeds \ CourseTableSeeder.php:15)
由于无法通过挖掘框架的源代码轻易地找出insert方法的工作方式,我开始怀疑是否DB::table('table_name')->insert()
通过数组接受了多个插入,并且设法在框架的测试套件中找到了该示例,例如:
DB::table('posts_tags')->insert([ ['post_id' => $post->id, 'tag_id' => 200, 'flag' => ''], ['post_id' => $post->id, 'tag_id' => 300, 'flag' => 'exclude'], ['post_id' => $post->id, 'tag_id' => 400, 'flag' => ''], ]);
来源:https : //github.com/laravel/framework/blob/5.6/tests/Integration/Database/EloquentBelongsToManyTest.php#L539
所以我想我没有做错任何事情。关于问题的根源以及如何调试出问题的任何想法,因为我已经用尽了我所知的所有选项。同样,在运行整个过程时,控制台或日志文件中不会显示任何错误。
谢谢!
您insert()
缺少某些[]
语法。比较你的
->insert( ["name" => "..."] );
到文档
->insert([ ["post_id" => "..."] ]);
因此,文档建议传递一个数组,在实现中传递多个数组时,该数组包含要插入的每个记录的数组。