database/seeders/RoleUserSeeder.php
/**
     * Run the database seeds.
     *
     * @return void
     */
Run the database seeds.
@return void
Run the database seeds . @return void
public function run()
    {
        DB::statement('SET FOREIGN_KEY_CHECKS=0');
        DB::table('role_user')->truncate();
        DB::statement('SET FOREIGN_KEY_CHECKS=1');
        $roleUsers = [
            [
                'user_email'=>'bob@sull.com',
                'role'=>'admin',
            ],
            [
                'user_email'=>'octo@sull.com',
                'role'=>'member',
            ],
            [
                'user_email'=>'partner@sull.com',
                'role'=>'affiliate',
            ],
        ];
        foreach ($roleUsers as &$data) {
            $user = User::firstWhere('email',$data['user_email']);
            $role = Role::firstWhere('role',$data['role']);
            unset($data['user_email']);
            unset($data['role']);
            $data['user_id'] = $user->id;
            $data['role_id'] = $role->id;
        }
        unset($data);
        DB::table('role_user')->insert($roleUsers);
    }
public function run ( ) { DB :: statement ( 'SET FOREIGN_KEY_CHECKS=0' ) ; DB :: table ( 'role_user' ) -> truncate ( ) ; DB :: statement ( 'SET FOREIGN_KEY_CHECKS=1' ) ; $ roleUsers = [ [ 'user_email' => 'bob@sull.com' , 'role' => 'admin' , ] , [ 'user_email' => 'octo@sull.com' , 'role' => 'member' , ] , [ 'user_email' => 'partner@sull.com' , 'role' => 'affiliate' , ] , ] ; foreach ( $ roleUsers as & $ data ) { $ user = User :: firstWhere ( 'email' , $ data [ 'user_email' ] ) ; $ role = Role :: firstWhere ( 'role' , $ data [ 'role' ] ) ; unset ( $ data [ 'user_email' ] ) ; unset ( $ data [ 'role' ] ) ; $ data [ 'user_id' ] = $ user -> id ; $ data [ 'role_id' ] = $ role -> id ; } unset ( $ data ) ; DB :: table ( 'role_user' ) -> insert ( $ roleUsers ) ; }
Run the database seeds.
Run the database seeds .
//Empty the table first //Define data //Prepare the data //Insert data in the table