一次种植多行laravel 5

时间:2022-12-02 17:23:31

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 array to create some fake data.

我正在尝试播种我的用户表。如果我这样尝试2行,就会失败。如果我只使用单个数组而不是$ users数组中的2个数组来创建一些假数据,它就可以正常工作。

What am I doing wrong, what is the proper way to do this?

我做错了什么,这样做的正确方法是什么?

class UserTableSeeder extends Seeder {

    public function run()
    {
        DB::table('users')->delete();

        $users = [
            ['id' => 1, 'name' => 'Stephan de Vries', 'username' => 'stephan', 'email' => 'stephan-v@gmail.com', 'password' => bcrypt('carrotz124')],
            ['id' => 2, 'name' => 'John doe', 'username' => 'johnny', 'email' => 'johndoe@gmail.com', 'password' => bcrypt('carrotz1243')],
        ];

        User::create($users);
    }

}

2 个解决方案

#1


38  

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()并插入:

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

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

实际上你也可以在模型上调用insert()(结果查询是相同的)

User::insert($users);

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

请注意,如果选择插入方法,则会丢失特殊的Eloquent功能,例如时间戳和模型事件。

#2


17  

This works, even for Laravel 5.3

这适用于Laravel 5.3

<?php

use 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')->get()->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 "; }

    }
}

#1


38  

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()并插入:

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

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

实际上你也可以在模型上调用insert()(结果查询是相同的)

User::insert($users);

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

请注意,如果选择插入方法,则会丢失特殊的Eloquent功能,例如时间戳和模型事件。

#2


17  

This works, even for Laravel 5.3

这适用于Laravel 5.3

<?php

use 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')->get()->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 "; }

    }
}