大佬教程收集整理的这篇文章主要介绍了php-使用属于用户的任务播种数据库,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我的laravel版本是Laravel Framework 5.5.22.
我有以下任务架构:
scheR_5_11845@a::create('tasks', function (Blueprint $tablE) {
$table->increments('id');
$table->String('name');
$table->Integer('user_id')->unsigned();
$table->timestamps();
});
在TaskTableSeeder类中,我想为每个用户分配一些任务.
请参阅下面我尝试的内容:
class TaskTableSeeder extends Seeder
{
public function run()
{
$faker = Faker\Factory::create();
$limit = 25;
$userIds = DB::table('users')->get('id');
for ($i = 0; $i < $limit; $i++) {
DB::table('tasks')->insert([
'name' => $faker->sentence(),
'user_id' => $faker->randomElement($userIds),
]);
}
}
}
但是,出现以下错误:
seeding: UserTableSeeder
seeding: TaskTableSeeder
In GrAMMar.PHP line 124:
Type error: Argument 1 passed to Illuminate\Database\GrAMMar::columnize() must be of the type array, String given, call
ed in C:\Users\marcus\Desktop\Coding Projects\demo_laravel_todo\vendor\laravel\framework\src\Illuminate\Database\Query\
GrAMMars\GrAMMar.PHP on line 131
有什么建议我做错了吗?
解决方法:
因为将字符串传递给randomElement(),所以得到了错误.您需要将数组传递给randomElement(),因此请使用pluck()和toArray():
$userIds = DB::table('users')->pluck('id')->toArray();
以上是大佬教程为你收集整理的php-使用属于用户的任务播种数据库全部内容,希望文章能够帮你解决php-使用属于用户的任务播种数据库所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。