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/"
}
},
CodePudding user response:
There are two options
You should put the
CountriesTableSeeder
in the seeders folder with the namespace ofnamespace Database\Seeders;
You should use the
CountriesTableSeeder
insideDatabaseSeeder
as belowuse 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),
]);
}
}
}