You can use it like this (if you extend the laravel test case)
$this->artisan('db:seed');
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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
You can use it like this (if you extend the laravel test case)
$this->artisan('db:seed');
how do i extend it? @sinnbeck
if I give him the following command he gets angry the idea gives it to me as an error
Artisan::call('db:seed');
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
{
OK but I didn't understand how to do the class... ? @sinnbeck
Show the code for the test in question please.
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
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.
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');
}
You seem to be mixing the two answers.. It is $this->artisan() not $this->call()
usual mistake.
So got it working?
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
PHP Fatal error: Uncaught ParseError: syntax error, @sinnbeck
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');
}
I did not understand where to put this function?
I tried to insert it in the test but it gives me an error @sinnbeck
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.
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');
}
}
the seed must start eventually @sinnbeck
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');
}
use RefreshDatabase; // or any other refreshing strategy (DatabaseMigrations, DatabaseTransactions)
public function setUp(): void
{
parent::setUp();
$this->seed(DatabaseSeeder::class); // or any other seeder class
}
Please or to participate in this conversation.