【问题标题】:Laravel 5.0 db:seed class not found fatal errorLaravel 5.0 db:找不到种子类致命错误
【发布时间】:2016-05-05 12:58:50
【问题描述】:

我已经尝试了我能想到的一切,但似乎无法解决这个问题,我确信这是我应该能够解决的小问题,但它让我很难过。

我已经运行 composer dump-autoload 但收效甚微。

每当我使用artisan db:seed 命令时,我都会收到以下错误:

PHP 致命错误:在中找不到类“App\Models\Company” X:\Development\laravelApplication\database\seeds\DatabaseSeeder.php 上 第 60 行

app/Http/Models/Company.php

<?php namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Company extends Model {

    //
    public function configurations() {
        return $this->hasMany('App\Models\Configuration');
    }
    public function users() {
        return $this->hasMany('App\Models\Users');
    }
    public function admins() {
        return $this->users()->where('admin', 1)->get();
    }

}

数据库/种子/DatabaseSeeder.php

<?php

use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
use App\Models\Company;

class DatabaseSeeder extends Seeder {

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Model::unguard();

        $this->call('CompaniesTableSeeder');
        $this->command->info('Companies table seeded!');

        $this->call('UserTableSeeder');
        $this->command->info('User table seeded!');
    }

}

/**
 * Seed the Users table
 * @return void
 */
class UserTableSeeder extends Seeder
{

    function run()
    {
        $needlers_id = Company::where('name', 'Needlers LTD')->first()->id;
        DB::table('users')->delete();
        User::create([
            'company_id' => $needlers_id,
            'name' => 'Joe Ware',
            'email' => 'joe.ware@needlers.co.uk',
            'password' => 'needlers123',
            'phone' => '07123456789',
            'admin' => 1,
            'screen' => 0,
        ]);
    }
}

/**
 * Seed the Users table
 * @return void
 */
class CompaniesTableSeeder extends Seeder
{

    function run()
    {
        DB::table('companies')->delete();
        Company::create([
            'name' => 'Company name',
            'tier' => 'platinum'
        ]);
    }
}

谁能指出我遗漏了什么/做错了什么?

【问题讨论】:

  • 请显示完整的DatabaseSeeder.php。另外,尝试运行composer dumpauto
  • @AlexeyMezenin 谢谢 Alexey,我正在编辑我的问题,所以当你发表评论时我已经运行 composer dump-autoload,我已经进一步编辑了我的问题以显示完整的播种机类

标签: php laravel laravel-5


【解决方案1】:

尝试使用这个:

Company::create([
        'name' => 'Needlers LTD',
        'tier' => 'platinum'
    ]);

或者:

\App\Models\Company::create([
            'name' => 'Needlers LTD',
            'tier' => 'platinum'
        ]);

而不是这个:

App\Models\Company::create([
        'name' => 'Needlers LTD',
        'tier' => 'platinum'
    ]);

另外,请确保您已自动加载模型命名空间,在 composer.json 中使用如下内容:

"autoload": {
    "classmap": [
        "database",
        "app/Models"
    ],

【讨论】:

  • 抱歉 App\Models\... 在我的编辑中仅存在,因为我仍在尝试自己解决这个问题,但最初并不存在。我已经尝试了您对 \App\Models\... 的建议,然后进行了 dumpauto,但仍然没有成功。
  • 太棒了,就是这样,我知道这将是我应该能够锻炼的东西!谢谢!
【解决方案2】:

试试看

composer dump

运行后。

php artisan db:seed

【讨论】:

    猜你喜欢
    • 2015-07-22
    • 2018-01-17
    • 1970-01-01
    • 2016-01-28
    • 2016-02-08
    • 1970-01-01
    • 2014-10-03
    • 2018-03-09
    • 2017-12-24
    相关资源
    最近更新 更多