1
votes

I'm trying to populate a Laravel 5.6 project DB - following the offial docs - without success. php artisan db:seed throws this exception:

Symfony\Component\Debug\Exception\FatalThrowableError : Class 'App\Item' not found

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

Exception trace:

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

I already tried most of the common suggestions provided from the community, like this one, as well as:

  • Trying with composer self-update + composer dump-autoload;
  • On my composer.json the autoload property is set as is:

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

(Tried to put classmap in autoload-dev too).

Here's the situation:

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);
    }
}
  • At last, I tried to put dependencies directly in sources too:

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

by removing the App\ prefix and leave only Item::class in the argument:

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

All these tries didn't helped, so I'm actually stuck. If anyone could show me the way, it should be really appreciated.

Thanks in advance to all.

UPDATE

@kerbholz & @h-h: There was a mistyped trait in ItemTableSeeder.php, thanks for both your suggestion. Yes, in first place I implemented an Item.php model like this:

<?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');
    }
}

Meanwhile I continued to implement others. Now, after correcting the mistype and run again twice a composer dump-autoload seeding started. It populated some tables, but after that thrown a new exception. Here's an extract from last try:

    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: In this case, I tried to put backslash before "App": \App\Item::class with no success. Dunno if it's related to some faker misconfiguration...

2
factory(App\Item::class, 25)->create(); should do it. Do you have a model App\Item?? – brombeer

2 Answers

2
votes

Found it.

Inside ItemFactory.php I put a stupid $this as factory parameter, in a relation creation:

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

By changing the return sentence in this way:

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

the issue seems to be solved.

Thanks everyone for the assistance.

0
votes

You need to either import the Item class like so:

use App\Item;

which means you can do this:

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

--

Or put a \ before hand like so:

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

--

Also make sure your Item class has this at the top:

namespace App;