mardi 13 septembre 2022

Laravel import Seed Class into Command [closed]

I want to execute a Seed Class by running a Laravel Command. My problem is that I can`t correctly import, so my code get thrown with

Target class [TestSeeder] does not exist. inside TestCommand.php.

I am on Laravel 5 and have created both classes via CLI make:seed and make:command, as result the classes are saved in the according folders. I have read some solutions regarding composer dump-autoload but this gets stuck in endless loop, maybe because its a huge codebase..

database\seeds\TestSeeder.php

<?php

use App\Models\Hotel;
use Illuminate\Database\Seeder;

class TestSeeder extends Seeder
{
    public function run(int $id)
    {
       // do stuff with {id}..
    }
}

app\console\commands\TestCommand.php

<?php

use Illuminate\Console\Command;
use Database\Seeds\TestSeeder;

class TestCommand extends Command
{

    protected $signature = 'test:data';

    protected $description = 'Add default data to the database for specific id via Seeder';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        $id = $this->ask('Please enter the id for which you want to add the seed data');

        if (!$id) {
            $this->error('No id given');
            return;
        }

        // $this->call(TestSeeder::class, ['id' => $id]);
        $testSeeder = new TestSeeder();
        $testSeeder->run($id);
    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire