Seed multiple rows at once laravel 5

前端 未结 6 1094
情深已故
情深已故 2021-02-01 12:44

I\'m currently trying to seed my users table. If I try it like this with 2 rows, it fails. It works fine if I just use a single array instead of the 2 arrays inside the $users a

6条回答
  •  隐瞒了意图╮
    2021-02-01 13:08

    If you have to use the model you need a loop:

    foreach($users as $user){
        User::create($user);
    }
    

    Otherwise you can just use DB::table() and insert:

    DB::table('users')->insert($users);
    

    Actually you can also call insert() on the model (the resulting query is the same)

    User::insert($users);
    

    Note if you choose the insert method you loose special Eloquent functionality such as timestamps and model events.

提交回复
热议问题