Search code examples
phplaravel-5.7laravel-seeding

Laravel Factories how to call them from your database seeder file


How to call the factorie files from the database seeder I looked at the laravel docs and was trying around whit this code

https://laravel.com/docs/5.7/seeding

   factory(App\User::class, 50)->create()->each(function ($user) {
        $user->posts()->save(factory(App\Post::class)->make());
    });

But it didnt work so mine question is how do i call mine factorie from mine database seeder file so i can execute them from the command line?

databaseseeder

  <?php

 use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
 {
/**
 * Seed the application's database.
 *
 * @return void
 */
public function run()
{
    Eloquent::unguard();

    $this->call(UserFactory::class);
    $this->call(PostFacory::class);
    $this->call(ProfileFacory::class);

    $this->command->info("Database seeded.");
    Eloquent::reguard();
  }
 }

user factorie

  <?php

  use Faker\Generator as Faker;

  $factory->define(App\User::class, function (Faker $faker) {
return [
    'name' => $faker->name,
    'email' => $faker->unique()->safeEmail,
    'email_verified_at' => now(),
    'password' => bcrypt('password'),
    'remember_token' => str_random(10),
];
});

Solution

  • Database seeding is

    php artisan db:seed  
    

    In the laravel path... also if you have already seeded you have to clean ( sure if do not have inportant data )

    php artisan migrate:refresh 
    

    Carefully here :)