Seed multiple rows at once laravel 5 Seed multiple rows at once laravel 5 php php

Seed multiple rows at once laravel 5


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.


This works, for Laravel ^5

<?phpuse Illuminate\Database\Seeder;class UsersTableSeeder extends Seeder{    /**     * Run the database seeds.     *     * @return void     */    public function run()    {        // check if table users is empty        if(DB::table('users')->count() == 0){            DB::table('users')->insert([                [                    'name' => 'Administrator',                    'email' => 'admin@app.com',                    'password' => bcrypt('password'),                    'created_at' => date('Y-m-d H:i:s'),                    'updated_at' => date('Y-m-d H:i:s'),                ],                [                    'name' => 'Agency',                    'email' => 'agency@app.com',                    'password' => bcrypt('password'),                    'created_at' => date('Y-m-d H:i:s'),                    'updated_at' => date('Y-m-d H:i:s'),                ],                [                    'name' => 'End',                    'email' => 'endcustomer@app.com',                    'password' => bcrypt('password'),                    'created_at' => date('Y-m-d H:i:s'),                    'updated_at' => date('Y-m-d H:i:s'),                ]            ]);                    } else { echo "\e[31mTable is not empty, therefore NOT "; }    }}


public function run(){    //    for ($i=0; $i < 1000; $i++) {          DB::table('seo_contents')->insert([            'title' => str_random(10),            'content' => str_random(100),            'created_at'=>date('Y-m-d H:i:s'),            'updated_at'=>date('Y-m-d H:i:s'),        ]);    }}