CountriesTableSeeder 不存在 - Laravel

CountriesTableSeeder does not exist - Laravel

https://i.stack.imgur.com/PkCwB.png : 错误截图

我已经用 composer 安装了一个包 ernysans/laraworld。在 app/config/app.php 中添加了相应的 providers & aliases。然后,我 运行 这个 - php artisan vendor:publish --provider="ErnySans\Laraworld\LaraworldServiceProvider" & php artisan migrate。之后,当 运行ning php artisan db:seed 时,出现上述错误。我已经经历过这个 link in laravel 8 with seeding , i has this issue Target class [TableSeeder] does not exist。试过 composer dump-autoload。我该如何解决这个问题?

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. 您应该将 CountriesTableSeeder 放在命名空间为

    的 seeders 文件夹中

    命名空间Database\Seeders;

  2. 你应该使用 DatabaseSeeder 里面的 CountriesTableSeeder 如下

    使用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]);
    }
}

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

解决方案 2 完整代码

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

使用以下内容更新您的代码:

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),
            ]);
        }
    }
}

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

解决方案 1 完整代码:

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

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]);
    }
}

注意:将您的 CountriesTableSeeder.php 移到 seeders 文件夹中

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),
            ]);
        }
    }
}