laravel - How can I maintain foreign keys when seeding database with Faker? -


below model factory.

$factory->define(app\business::class, function (faker\generator $faker){ return [ 'name' => $faker->bs, 'slug' => $faker->slug, 'address' => $faker->streetaddress, 'phone_no' => $faker->phonenumber, 'mobile_no' => $faker->phonenumber, 'email' => $faker->companyemail, 'website' => $faker->domainname, 'latitude' => $faker->latitude, 'longitude' => $faker->longitude, 'location' => $faker->city, 'business_days_from' => $faker->dayofweek, 'business_days_to' => $faker->dayofweek, 'description' => $faker->text, 'user_id' => $faker->factory(app\user::class), ]; 

});

and database seeder class

class databaseseeder extends seeder {     public function run()     {         factory(app\business::class, 300)->create();     } } 

but when execute php artisan db:seed ...it not work..

what should workaround here..any appreciated..

i found workaround .. replaced

'user_id' => $faker->factory(app\user::class), 

with

'user_id' => $faker->randomelement(user::lists('id')->toarray()), 

and solves problem now..


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -