在种类中找不到Laravel 5.5模型

时间:2017-11-10 22:33:24

标签: php laravel laravel-5

我已经解决了这个问题很长时间并且无法解决它...我发现有几个人(显然)和我一样的问题,但任何答案对我有帮助。

我有以下" Sector.php"里面" app"文件夹:

<?php
namespace App;

use Illuminate\Database\Eloquent\Model;

class Sector extends Model
{
    protected $table    = 'sectors';
    protected $fillable = ['name'];
    protected $guarded  = ['id'];

    public function services()
    {
        return $this->belongsToMany('App\Service', 'services_sectors', 'sector_id', 'service_id');
    }

    public function observations()
    {
        return $this->belongsToMany('App\Observation', 'observations_sectors', 'sector_id', 'observation_id');
    }
}

以下&#34; DatabaseSeeder.php&#34;内部&#34;数据库/种子&#34;:

<?php

use Illuminate\Database\Seeder;


class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::table('sectors')->delete();

        Sector::create(['name' => 'Health']);

        $this->command->info('Sectors table seeded');
    }
}

因此,当我访问我的服务器时,我运行命令php artisan db:seed,但我有以下错误:

 [Symfony\Component\Debug\Exception\FatalThrowableError]  
  Class 'Sector' not found

我一直在尝试./composer update./composer dump-autoload -o,在播种机文件中将Sector更改为App\Sector,但错误只是更改为Class 'App\Sector' not found

如果我将use App\Sector;添加到Seeder文件的顶部,则错误是相同的。

我似乎尝试了所有在线解决方案,所以也许我的配置不正确?对此有何建议?

1 个答案:

答案 0 :(得分:1)

尝试将use App\Sector;添加到您的种子文件中。

让它工作后,考虑将种子文件分成不同的类。保持这种方式要容易得多。

生成播种文件

首先,在终端中,生成一个新的种子文件:

php artisan make:seeder SectorsTableSeeder

将种子代码转移到此新文件的run方法中。

调用播种机文件

然后,修改DatabaseSeeder.php文件以运行SectorsTableSeeder类。例如:

public function run()
    {
        $this->call(SectorsTableSeeder::class);

    }

<强>更新

对不起,我错过了那部分。

这就是我想要的:

$now = date('Y-m-d H:i:s');

public function run()
    {
        DB::table('sectors')->delete();

        DB::table('sectors')->insert([
            'name' => 'Health',
            'created_at' => $now,
            'updated_at' => $now,
        ]);

        $this->command->info('Sectors table seeded');
    }