How to realize the Seed of a Factory within another class with Phpunit/Laravel?

Asked

Viewed 236 times

0

I was studying the framework Laravel, and I was left with the following doubt: there is a way to realize the Seed of a factory within a common project class?

It would be like calling command php artisan db:seed --class=TabelaSeeder, but within a project class, and not in the terminal.

1 answer

1


I really like the Laravel framework for having a documentation excellent that has always met my needs. For your case, there is a paragraph (Programmatically Executing Commands) which explains how you can use Facade Artisan.

An example to run db:seed anywhere your project is as follows:

public function initialize()
{
    // Rollback em todas as migrações e migrar as tabelas novamente
    Artisan::call('migrate:refresh');

    // Alimentar as tabelas
    Artisan::call('db:seed', ['--class' => 'TabelaSeeder']);
}

Or you can pass the entire command as a string:

Artisan::call('db:seed --class=TabelaSeeder');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.