Alexander Kim
Alexander Kim

Reputation: 18410

Laravel. Database seeder multiple rows not working

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!

Upvotes: 1

Views: 3134

Answers (2)

Soniya Basireddy
Soniya Basireddy

Reputation: 369

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
}
}

Upvotes: 1

Mike Harrison
Mike Harrison

Reputation: 1393

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.

Upvotes: 0

Related Questions