代码之家  ›  专栏  ›  技术社区  ›  Luca Cattide

Laravel 5.6-找不到播种机类

  •  1
  • Luca Cattide  · 技术社区  · 7 年前

    我正在尝试填充一个laravel 5.6项目数据库-遵循 offial docs -没有成功。 php artisan db:seed 引发此异常:

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

    at/Applications/MAMP/htdocs/greylab/inventario/greylab-inventario/vendor/laravel/framework/src/Illuminate/Database/elokent/FactoryBuilder.php:217

    异常跟踪:

    1说明\数据库\雄辩\ FactoryBuilder::make([]) /应用程序/MAMP/htdocs/greylab/inventario/greylab_inventario/vendor/laravel/framework/src/Illuminate/Database/elokent/FactoryBuilder.php:167

    2说明\数据库\雄辩\ FactoryBuilder::创建() /应用程序/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/" } },

    (也尝试将类映射放入自动加载开发程序中)。

    情况是这样的:

    项目工厂.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();
        }
    }
    

    数据库播种器.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');
        }
    }
    

    与此同时,我继续实施其他措施现在,在纠正了错误的类型并每年运行两次之后 作曲家转储自动加载 开始播种。它填充了一些表,但之后抛出了一个新的异常。以下是最后一次尝试的摘录:

        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 配置错误。。。

    2 回复  |  直到 7 年前
        1
  •  0
  •   H H    7 年前

    您需要导入 Item 像这样的班级:

    use App\Item;

    也就是说你可以这样做:

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

    --

    或者放一个 \ 在这样做之前:

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

    --

    同时确保 项目 类的顶部有:

    namespace App;

        2
  •  0
  •   Luca Cattide    7 年前

    找到了。

    内部 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;
    

    这个问题似乎已经解决了。

    谢谢大家的帮助。

    推荐文章