【问题标题】:Laravel 5.6 - Seeder Class not foundLaravel 5.6 - 找不到播种机类
【发布时间】:2018-12-27 12:22:54
【问题描述】:

我正在尝试填充 Laravel 5.6 项目数据库 - 遵循 offial docs - 但没有成功。 php artisan db:seed 抛出这个异常:

Symfony\Component\Debug\Exception\FatalThrowableError:找不到类“App\Item”

在 /Applications/MAMP/htdocs/greylab/inventario/greylab_inventario/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:217

异常跟踪:

1 Illuminate\Database\Eloquent\FactoryBuilder::make([]) /Applications/MAMP/htdocs/greylab/inventario/greylab_inventario/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:167

2 Illuminate\Database\Eloquent\FactoryBuilder::create() /Applications/MAMP/htdocs/greylab/inventario/greylab_inventario/database/seeds/ItemTableSeeder.php:14

我已经尝试了社区提供的大部分常见建议,例如this one,以及:

  • 尝试使用composer self-update + composer dump-autoload;
  • 在我的composer.json 上,autoload 属性按原样设置:

"autoload": { "classmap": [ "database/seeds", "database/factories" ], "psr-4": { "App\\": "app/" } },

(也尝试将 classmap 放入 autoload-dev)。

情况如下:

ItemFactory.php

<?php

use Faker\Generator as Faker;

// Definizione dati test
$factory->define(App\Item::class, function (Faker $faker) {
    return [ ...]
}

ItemTableSeeder.php

<?php

use Illuminate\Database\Seeder;

class ItemTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        factory(App\Item::class, 25)->create();
    }
}

DatabaseSeeder.php

<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        $this->call(ItemTableSeeder::class);
    }
}
  • 最后,我也尝试将依赖项也直接放在源中:

use App\Item; use Illuminate\Database\Seeder;

通过删除 App\ 前缀并在参数中仅保留 Item::class:

factory(Item::class, 25)->create();

所有这些尝试都没有帮助,所以我实际上被卡住了。 如果有人能给我指路,那将不胜感激。

在此先感谢大家。

更新

@kerbholz & @h-h:ItemTableSeeder.php 中有一个错误的特征,感谢您的建议。是的,首先我实现了一个像这样的Item.php 模型:

<?php

// Definizione Namespace
namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * Classe Item
 */
class Item extends Model
{
    use SoftDeletes;

    // Dichiarazione Proprietà
    protected $table = 'item';
    protected $dateformat = 'Y-m-d';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'data_acquisto',
        'labeled',
        'estensione_garanzia',
        'stato',
        'data_dismissione',
        'note'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'codice',
        'serial',
        'componente_id',
        'tipologia_id',
        'condizione_id',
        'locazione_id',
        'fornitore_id',
        'parent_id'
    ];

    /**
     * The attributes that should be mutated to dates.
     *
     * @var array
     */
    protected $dates = [
        'data_acquisto',
        'data_dismissione',
        'deleted_at'
    ];

    /**
     * All of the relationships to be touched.
     *
     * @var array
     */
    protected $touches = [
        'componenti',
        'condizioni',
        'fornitori',
        'locazioni',
        'tipologie'
    ];

    /**
     * Scope query item figli
     * Getter
     * @param array $query Query
     * @return array Query
     */
    public function scopeFigli($query)
    {
        return $query->where('parent_id', '!=', null);
    }

    /**
     * Componenti Correlati
     * Getter
     * @return object Componenti
     */
    public function componenti()
    {
        // Definizione relazione
        return $this->belongsTo('App\Componente');
    }

    /**
     * Condizioni Correlate
     * Getter
     * @return object Condizioni
     */
    public function condizioni()
    {
        // Definizione relazione
        return $this->belongsTo('App\Condizione');
    }

    /**
     * Fornitori Correlati
     * Getter
     * @return object Fornitori
     */
    public function fornitori()
    {
        // Definizione relazione
        return $this->belongsTo('App\Fornitore');
    }

    /**
     * Locazioni Correlate
     * Getter
     * @return object Locazioni
     */
    public function locazioni()
    {
        // Definizione relazione
        return $this->belongsTo('App\Locazione');
    }

    /**
     * Tipologie Correlate
     * Getter
     * @return object Tipologie
     */
    public function tipologie()
    {
        // Definizione relazione
        return $this->belongsTo('App\Tipologia');
    }
}

与此同时,我继续实施其他人。现在,在更正错误类型并再次运行两次之后,composer dump-autoload 播种开始了。它填充了一些表,但之后引发了一个新异常。这是上次尝试的摘录:

    Seeding: ItemTableSeeder

   ErrorException  : Illegal offset type

  at /Applications/MAMP/htdocs/greylab/inventario/greylab_inventario/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:257
    253|      * @throws \InvalidArgumentException
    254|      */
    255|     protected function getRawAttributes(array $attributes = [])
    256|     {
   257|         if (! isset($this->definitions[$this->class][$this->name])) {

@h-h:在这种情况下,我尝试在“App”之前加上反斜杠:\App\Item::class,但没有成功。不知道是否与某些 faker 配置错误有关...

【问题讨论】:

  • factory(App\Item::class, 25)-&gt;create(); 应该这样做。你有模型 App\Item 吗??

标签: php laravel laravel-5


【解决方案1】:

找到了。

在ItemFactory.php 内部,我在关系创建中放置了一个愚蠢的$this 作为工厂参数:

$factory->define(App\Item::class, function (Faker $faker) {
[...]
 'parent_id' => function() {
            return factory($this)->create()->id;
        }
}

通过这种方式改变return句子:

return factory(App\Item::class)->create()->id;

问题似乎解决了。

感谢大家的帮助。

【讨论】:

    【解决方案2】:

    您需要像这样导入Item 类:

    use App\Item;

    这意味着你可以这样做:

    factory(Item::class, 25)-&gt;create();

    --

    或者像这样在手前放一个\:

    factory(\App\Item::class, 25)-&gt;create();

    --

    还要确保你的 Item 类在顶部有这个:

    namespace App;

    【讨论】:

      猜你喜欢
      • 2014-12-08
      • 2019-11-02
      • 2014-09-05
      • 2013-05-25
      • 2019-11-29
      • 2021-03-19
      • 2022-11-27
      • 2018-12-09
      相关资源
      最近更新 更多