1

https://i.stack.imgur.com/PkCwB.png : Error Screenshot

i've installed a package ernysans/laraworld with composer. Added respective providers & aliases in app/config/app.php. Then, i run this - php artisan vendor:publish --provider="ErnySans\Laraworld\LaraworldServiceProvider" & php artisan migrate. After that, when running php artisan db:seed,am getting the above error. i've already gone through this link in laravel 8 with seeding , i has this issue Target class [TableSeeder] does not exist .Tried composer dump-autoload. How can i fix this?

database/seeders/DatabaseSeeder.php

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Database\Seeds\CountriesTableSeeder;

class DatabaseSeeder extends Seeder
{
    public function run()
    {
        $this->call([CountriesTableSeeder::class,TimeZonesTableSeeder::class,LanguagesTableSeeder::class]);
    }
}

database/seeds/CountriesTableSeeder.php

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use ErnySans\Laraworld\Models\Countries;

class CountriesTableSeeder extends Seeder
{        
    public function run()
    { 
        Countries::truncate();

        $JSON_countries = Countries::allJSON();

        foreach ($JSON_countries as $country) {
            Countries::create([
                'capital'           => ((isset($country['capital'])) ? $country['capital'] : null),
                'citizenship'       => ((isset($country['citizenship'])) ? $country['citizenship'] : null),
                'country_code'      => ((isset($country['country_code'])) ? $country['country_code'] : null),
                'currency'          => ((isset($country['currency'])) ? $country['currency'] : null),
                'currency_code'     => ((isset($country['currency_code'])) ? $country['currency_code'] : null),
                'currency_sub_unit' => ((isset($country['currency_sub_unit'])) ? $country['currency_sub_unit'] : null),
                'full_name'         => ((isset($country['full_name'])) ? $country['full_name'] : null),
                'iso_3166_2'        => ((isset($country['iso_3166_2'])) ? $country['iso_3166_2'] : null),
                'iso_3166_3'        => ((isset($country['iso_3166_3'])) ? $country['iso_3166_3'] : null),
                'name'              => ((isset($country['name'])) ? $country['name'] : null),
                'region_code'       => ((isset($country['region_code'])) ? $country['region_code'] : null),
                'sub_region_code'   => ((isset($country['sub_region_code'])) ? $country['sub_region_code'] : null),
                'eea'               => (bool)$country['eea'],
                'calling_code'      => ((isset($country['calling_code'])) ? $country['calling_code'] : null),
                'currency_symbol'   => ((isset($country['currency_symbol'])) ? $country['currency_symbol'] : null),
            ]);
        }
    }
}

composer.json

 "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        }
    },
 

1 Answers1

1

There are two options

  1. You should put the CountriesTableSeeder in the seeders folder with the namespace of

    namespace Database\Seeders;

  2. You should use the CountriesTableSeeder inside DatabaseSeeder as below

    use Database\Seeds\CountriesTableSeeder;

database/seeder/DatabaseSeeder.php

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Database\Seeds\CountriesTableSeeder;

class DatabaseSeeder extends Seeder
{
    public function run()
    {
        $this->call([CountriesTableSeeder::class,TimeZonesTableSeeder::class,LanguagesTableSeeder::class]);
    }
}

=============================================

Solution 2 Complete Code

=============================================

Update your code with below contents:

database/seeders/DatabaseSeeder.php

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Database\Seeds\CountriesTableSeeder;

class DatabaseSeeder extends Seeder
{
    public function run()
    {
        $this->call([CountriesTableSeeder::class,TimeZonesTableSeeder::class,LanguagesTableSeeder::class]);
    }
}

database/seeds/CountriesTableSeeder.php

<?php

namespace Database\Seeds;

use Illuminate\Database\Seeder;
use ErnySans\Laraworld\Models\Countries;

class CountriesTableSeeder extends Seeder
{        
    public function run()
    { 
        Countries::truncate();

        $JSON_countries = Countries::allJSON();

        foreach ($JSON_countries as $country) {
            Countries::create([
                'capital'           => ((isset($country['capital'])) ? $country['capital'] : null),
                'citizenship'       => ((isset($country['citizenship'])) ? $country['citizenship'] : null),
                'country_code'      => ((isset($country['country_code'])) ? $country['country_code'] : null),
                'currency'          => ((isset($country['currency'])) ? $country['currency'] : null),
                'currency_code'     => ((isset($country['currency_code'])) ? $country['currency_code'] : null),
                'currency_sub_unit' => ((isset($country['currency_sub_unit'])) ? $country['currency_sub_unit'] : null),
                'full_name'         => ((isset($country['full_name'])) ? $country['full_name'] : null),
                'iso_3166_2'        => ((isset($country['iso_3166_2'])) ? $country['iso_3166_2'] : null),
                'iso_3166_3'        => ((isset($country['iso_3166_3'])) ? $country['iso_3166_3'] : null),
                'name'              => ((isset($country['name'])) ? $country['name'] : null),
                'region_code'       => ((isset($country['region_code'])) ? $country['region_code'] : null),
                'sub_region_code'   => ((isset($country['sub_region_code'])) ? $country['sub_region_code'] : null),
                'eea'               => (bool)$country['eea'],
                'calling_code'      => ((isset($country['calling_code'])) ? $country['calling_code'] : null),
                'currency_symbol'   => ((isset($country['currency_symbol'])) ? $country['currency_symbol'] : null),
            ]);
        }
    }
}

=================================

Solution 1 Complete Code:

=================================

database/seeders/DatabaseSeeder.php

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    public function run()
    {
        $this->call([CountriesTableSeeder::class,TimeZonesTableSeeder::class,LanguagesTableSeeder::class]);
    }
}

NOTE: Move your CountriesTableSeeder.php inside the seeders folder

database/seeders/CountriesTableSeeder.php

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use ErnySans\Laraworld\Models\Countries;

class CountriesTableSeeder extends Seeder
{        
    public function run()
    { 
        Countries::truncate();

        $JSON_countries = Countries::allJSON();

        foreach ($JSON_countries as $country) {
            Countries::create([
                'capital'           => ((isset($country['capital'])) ? $country['capital'] : null),
                'citizenship'       => ((isset($country['citizenship'])) ? $country['citizenship'] : null),
                'country_code'      => ((isset($country['country_code'])) ? $country['country_code'] : null),
                'currency'          => ((isset($country['currency'])) ? $country['currency'] : null),
                'currency_code'     => ((isset($country['currency_code'])) ? $country['currency_code'] : null),
                'currency_sub_unit' => ((isset($country['currency_sub_unit'])) ? $country['currency_sub_unit'] : null),
                'full_name'         => ((isset($country['full_name'])) ? $country['full_name'] : null),
                'iso_3166_2'        => ((isset($country['iso_3166_2'])) ? $country['iso_3166_2'] : null),
                'iso_3166_3'        => ((isset($country['iso_3166_3'])) ? $country['iso_3166_3'] : null),
                'name'              => ((isset($country['name'])) ? $country['name'] : null),
                'region_code'       => ((isset($country['region_code'])) ? $country['region_code'] : null),
                'sub_region_code'   => ((isset($country['sub_region_code'])) ? $country['sub_region_code'] : null),
                'eea'               => (bool)$country['eea'],
                'calling_code'      => ((isset($country['calling_code'])) ? $country['calling_code'] : null),
                'currency_symbol'   => ((isset($country['currency_symbol'])) ? $country['currency_symbol'] : null),
            ]);
        }
    }
}
dev_mustafa
  • 1,042
  • 1
  • 4
  • 14
  • Am getting the same error. See the updated questin body.I've made the changes u said as in it –  Sep 24 '21 at 05:31
  • you have used both the solutions if you have moved the `CountriesTableSeeder` inside the `seeders` folder you can remove the `use Database\Seeds\CountriesTableSeeder;` from `DatabaseSeeder` – dev_mustafa Sep 24 '21 at 05:33
  • Now , i just deleted `CountriesTableSeeder` from `seeders` folder & put it in `seeds` folder. And kept the line of `use Database\Seeds\CountriesTableSeeder;` in `databaseSeeder.php`. Still am having the same error. if so, what should be the namespace of `coutriesTableSeeder` now? –  Sep 24 '21 at 05:37
  • I've added a complete code section for you in my answer check that... – dev_mustafa Sep 24 '21 at 05:42
  • I pasted ur code. Still same error. –  Sep 24 '21 at 05:46
  • Did you add the autoloads to the `composer.json`? see https://stackoverflow.com/a/65377397/3330212 – Anthony Aslangul Sep 24 '21 at 05:49
  • @toyi yes. see the updated question body. i've added the composer.son content in it –  Sep 24 '21 at 05:52
  • I've added another section in my answer with the title `Solution 1 Complete Code:` use that and also run `composer dump-autoload` after the changes – dev_mustafa Sep 24 '21 at 05:53