1

I want to seed database with multiple rows with a seeder:

public function run()   {
    DB::table('users')->insert([

        [
            'name' => 'Guest',
            'surname' => 'Guest',
            'email' => '[email protected]',
            'phone' => '+777',
            'password' => bcrypt('password'),
            'is_admin' => false,
        ],
        [
            'name' => 'Alexander',
            'surname' => 'Jones',
            'email' => '[email protected]',
            'phone' => '+12321321312',
            'password' => bcrypt('password'),
            'is_admin' => true,
        ]

    ]);

However when i run php artisan db:seed it seeds only with the first row. How can i make seeder for multiple rows? L5.2 documentation lacks this kind of information. Please help!

9
  • Instead of use DB, can you use App\User .. and do something like User::insert(). Does that work? Commented Dec 12, 2016 at 18:19
  • This should work. Commented Dec 12, 2016 at 18:20
  • @TheAlpha: you mean my code should work? But in fact, it doesn't... To prateekkathal: it didn't work, same - just 1 row, instead of 2. Commented Dec 12, 2016 at 18:21
  • Are you getting any errors in storage/logs/laravel.log? Commented Dec 12, 2016 at 18:25
  • @MikeHarrison I am getting errors there, but not related to a seeder. Commented Dec 12, 2016 at 18:27

2 Answers 2

1
Try this once..

$mul_rows= [
    [ 'name' => 'Guest',
            'surname' => 'Guest',
            'email' => '[email protected]',
            'phone' => '+777',
            'password' => bcrypt('password'),
            'is_admin' => false,],
    [  'name' => 'Alexander',
            'surname' => 'Jones',
            'email' => '[email protected]',
            'phone' => '+12321321312',
            'password' => bcrypt('password'),
            'is_admin' => true,]
];

foreach ($mul_rows as $rows) {
   //$insert = DB::table('departments')->insert($mul_rows); old
   $insert= DB::table('users')->insert($rows);
if($insert){
//success message here
}else{
//Failure message here
}
}
Sign up to request clarification or add additional context in comments.

Comments

0

Through chat we discovered that the rows were actually being inserted but when calling to the DB through w/e tool the OP was using was only returning 1 result at a time which made it appear.

For other users using Sequel Pro (https://sequelpro.com/) I suggest looking at the table view and not running queries as that may be misleading. Not entirely sure but we think that it is adding a limit of 1 to the end of the query.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.