一次播种多行laravel 5

lf5gs5x2  于 2023-03-31  发布在  其他
关注(0)|答案(7)|浏览(122)

我正在尝试为我的users表播种。如果我这样尝试使用2行,它会失败。如果我只使用单个数组而不是$users数组中的2个数组来创建一些假数据,它会很好地工作。
我做错了什么,正确的方法是什么?

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

}
0vvn1miw

0vvn1miw1#

如果你必须使用模型,你需要一个循环:

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

否则,您可以使用DB::table()insert

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

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

User::insert($users);

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

cwdobuhd

cwdobuhd2#

这对Laravel来说是可行的^5

<?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')->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 "; }

    }
}
kmpatx3s

kmpatx3s3#

create()只用于存储一条记录。要存储多条记录,您应该使用insert()而不是create()。因此代码如下所示:

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::insert($users);
  }

}

P.S. insert()函数不会存储时间戳。即created_byupdated_by字段。

ndh0cuux

ndh0cuux4#

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'),

        ]);
    }

}
dfuffjeb

dfuffjeb5#

使用截断

<?php

use Illuminate\Database\Seeder;
use App\User;

class UsersTableSeeder extends Seeder
{
     /**
     * Run the database seeds.
     *
     * @return void
     */

  public function run()
    {

      User::truncate();

        $users =  [
            [
              'name' => 'Super Admin',
              'email' => 'superadmin@gmail.com',
              'password' => '123456',
            ],
            [
              'name' => 'Account Admin',
              'email' => 'accountadmin@gmail.com',
              'password' => '13456',
            ],
            [
              'name' => 'Project Admin',
              'email' => 'projectadmin@gmail.com',
              'password' => '13456',
            ],
            [
              'name' => 'Client Admin',
              'email' => 'clientadmin@gmail.com',
              'password' => '13456',
            ]
          ];

          User::create($users);

    }
}
oxosxuxt

oxosxuxt6#

如果有人正在努力解决这个问题,我从Laravel 5开始就一直在使用以下内容,并且可以确认仍然在**Laravel 7+**中工作。

class UserTableSeeder extends Seeder {

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

    \DB::table('users')->insert(array (
        0 => 
          array (
                 'id' => 1,
                 'name' => 'Stephan de Vries',
                 'username' => 'stephan',
                 'email' => 'stephan-v@gmail.com',
                 'password' => bcrypt('carrotz124'
         ),
        1 => 
          array (
                 'id' => 2,
                 'name' => 'John doe',
                 'username' => 'johnny',
                 'email' => 'johndoe@gmail.com',
                 'password' => bcrypt('carrotz1243'
         ),
     ));

}}
s6fujrry

s6fujrry7#

我这样做是为了插入新的和更新以前的记录每次播种机运行。

$languages = [
        ['id' => 1, 'language_name' => 'English', 'language_code' => 'en'],
        ['id' => 2, 'language_name' => 'Spanish', 'language_code' => 'es'],
        ['id' => 3, 'language_name' => 'French', 'language_code' => 'fr'],
        ['id' => 4, 'language_name' => 'German', 'language_code' => 'de'],
        ['id' => 5, 'language_name' => 'Swahili', 'language_code' => 'sw']
    ];

    foreach ($languages as $language) {
        Language::updateOrCreate(['id' => $language['id']], $language);
    }

相关问题