FatalThrowableError when using DatabaseSeeder, to populate

0

I'm doing a test, to populate the bd, using DatabaseSeeder. I have an individual VehiculosSeeder file, a Vehiculo model, linked to the vehicles table, and when I try to run from phisic artisan db: seed console, I get this:

    Symfony\Component\Debug\Exception\FatalThrowableError  : Class 'Model'      not found

  at C:\xampp\htdocs\logismart\database\seeds\DatabaseSeeder.php:14
    10|      * @return void
    11|      */
    12|     public function run()
    13|     {
  > 14|        Model::unguard();
    15|
    16|            $this->call(VehiculosSeeder::class);  // ejemplo ejecucion seeder
    17|                                                 //$this->call(UsersTableSeeder::class);
    18|        Model::reguard();

  Exception trace:

  1   DatabaseSeeder::run()
      C:\xampp\htdocs\logismart\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:29

  2   call_user_func_array([])
      C:\xampp\htdocs\logismart\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:29. 

My vehicle model is like this:

    use Illuminate\Database\Eloquent\Model;
use App\Vehiculo;

class Vehiculo extends Model
{
    protected $table = 'vehiculos';
    protected $fillable = array ['id', 'matricula', 'marca', 'capacidadCarga', 'añoFabricacion', 'color'];


    public function ordenes
        {   
            return $this->hasOne('ordenesCargas');

        }
}

My individual seeder VehiculosSeeder is like this:

<?php

use Illuminate\Database\Seeder;

class VehiculosSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
         Vehiculo::create([
             'id'    =>    001,
             'matricula'   =>    09890,
             'marca'   =>    Volkswagen,
             'capacidadCarga'   =>    20,
             'añoFabricacion'   =>    2001,
             'color'   =>    blanco,

         ])   //
    }
}

DatabaseSeeder stays like this:

use Illuminate\Database\Seeder;

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

           $this->call(VehiculosSeeder::class);  // ejemplo ejecucion seeder
                                                //$this->call(UsersTableSeeder::class);
       Model::reguard();     
    }
}

Can you tell me what I'm doing wrong? Thanks

    
asked by FGB 17.04.2018 в 17:50
source

1 answer

0

If your id field is autoincrement you should not even include it in your seeder and the other detail is that you must pass the values of your column with the type of data it is.

  Vehiculo::create([
         'matricula'   =>    '09890',
         'marca'   =>    'Volkswagen',
         'capacidadCarga'   =>    20,
         'añoFabricacion'   =>    2001,
         'color'   =>    'blanco',

     ]);

I imagine that the registration field is a string because it starts with 0 would be a little orthodox to keep an integer with a 0 in front, it would not make sense, anyway you can take the idea.

And finally use the using to avoid error when using the class Model

use Illuminate\Database\Eloquent\Model;
    
answered by 17.04.2018 / 18:07
source