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 -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -