Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Antonella's avatar

launch from php code artisan db: seed

how can i run the command in the code:

php artisan db:seed 

I would need it to be able to repole the db at the end of each test

0 likes
22 replies
Sinnbeck's avatar

You can use it like this (if you extend the laravel test case)

$this->artisan('db:seed');
Antonella's avatar

if I give him the following command he gets angry the idea gives it to me as an error

Artisan::call('db:seed');

@michaloravec

Sinnbeck's avatar

In the tests folder there should be a TestCase.php file you can extend in your tests. That one already extends the laravel one

So you test should look something like this

<?php

namespace Tests\Feature;

use Tests\TestCase;

class ExampleTest extends TestCase
{
Sinnbeck's avatar

Show the code for the test in question please.

Antonella's avatar

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;

class SeedCommand extends Command
{

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'seed {--database=} {--path=} {--force} {--seed} {--class=}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {

        Artisan::call('db:seed');
    }
}

unfortunately when I go to call him back in the test he gives me an error:

$this->call('db:seed');//ide marks this as an error

@sinnbeck

Sinnbeck's avatar

That is a command.. You asked how to call it in a test not a command?

... the db at the end of each test

Show the test, if that was what you meant.

Antonella's avatar

namespace Tests\Feature; use App\Models\Task; use App\Models\User; use Laravel\Sanctum\Sanctum; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; use Illuminate\Support\Facades\Storage; use Tests\TestCase; use App\Console\Commands\SeedCommand;

class ApiTest extends TestCase
{

    $this->call('db:seed');

}

@sinnbeck

Sinnbeck's avatar

You seem to be mixing the two answers.. It is $this->artisan() not $this->call()

Antonella's avatar

if you can show me how you would have extended the test case to do db: seed? that I understand well how to implement it @sinnbeck

Sinnbeck's avatar

Sure. This is from an actual project. But it is inside the TestCase.php file as it runs in all tests.

public function setUp(): void
    {
        parent::setUp();
        $this->artisan('migrate:fresh', ['--path' => 'database/migrations', '--database' => 'testing']);
        $this->artisan('db:seed --class=TestingSeeder');
    }
Antonella's avatar

I did not understand where to put this function?

I tried to insert it in the test but it gives me an error @sinnbeck

Sinnbeck's avatar

This is my test/TestCase.php

<?php

namespace Tests;

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;

    public function setUp(): void
    {
        parent::setUp();
        $this->artisan('migrate:fresh', ['--path' => 'database/migrations', '--database' => 'testing']);
        $this->artisan('db:seed --class=TestingSeeder');
    }
}

And please, if you get an error, post the error.. Us having to guess it, helps no one.

Antonella's avatar
  1. Tests\Feature\updateErrorApiTest::testNotAuthorizedIdSUpdateErrorApiHoqu InvalidArgumentException: Database connection [testing] not configured.

i put the code as follows

    public function testSushi()
    {
        $user_tokens = json_decode(Storage::get('test_data/tokens_users.json'),TRUE);

        //add data with api/queues
        $data = [
            "poll_in" => "https:\/\/ok.org",
            "category" => "sushi",
            "param" => ["a"=> "yes", "b"=> "no", "c" => "so and so", "d"=>"maybe"],
        ];
        $response = $this->withHeaders([
            'Accept' => 'application/json',
            'Authorization' => 'Bearer '.$user_tokens['[email protected]']
        ])->post('/api/store',$data);
        $response->assertStatus(201);

    }




    public function setUp(): void
    {
        parent::setUp();
        $this->artisan('migrate:fresh', ['--path' => 'database/migrations', '--database' => 'testing']);
        $this->artisan('db:seed');
    }



}
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Well mine was just an example from my actual code... Your project might not be exactly set up the same as mine.. I would assume this would be enough.

public function setUp(): void
    {
        parent::setUp();
        $this->artisan('migrate:fresh --seed');
    }
bugsysha's avatar

@gianmarx

use RefreshDatabase; // or any other refreshing strategy (DatabaseMigrations, DatabaseTransactions)

public function setUp(): void
{
  parent::setUp();
  $this->seed(DatabaseSeeder::class); // or any other seeder class
}
1 like

Please or to participate in this conversation.