luqmanrom
5/17/2016 - 2:55 AM

Seed many to many relationship

Seed many to many relationship

    public function run()
    {

    	DB::statement('SET FOREIGN_KEY_CHECKS=0;');

    	DB::table('user_account')->truncate();
        
		$users    = \App\User::all();
		$account_ids = \DB::table('accounts')->select('id')->lists('id');

		foreach($users as $user) {
			$user->accounts()->sync(array_rand($account_ids,2));
		}

    	DB::statement('SET FOREIGN_KEY_CHECKS=1;');
}