error when using factory faker

0

Hi, I'm trying to use faker, to feed my bd, but it throws me the following error:

  

ReflectionException: Class CarnetSeeder does not exist

I show you below how I have the files My table

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCarnetsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('carnets', function (Blueprint $table) {
            $table->increments('id');
            $table->string('tipo',20);
            $table->date('fechaExpedicion');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('carnets');
    }
}

my model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Carnet extends Model
{
   //define la tabla a que pertenece y los campos rellenables
    protected $table = 'carnets';
    protected $fillable = ['id','tipo','fechaExpedicion'];

    //define la relacion entre las tablas
    public function relCarnet()
        {   
            return $this->hasMany('choferes');

        }


    public function mostrarTodos() //Muestra todos los carnets existentes
        {


        }
}

The factory

<?php

use Faker\Generator as Faker;

/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| This directory should contain each of the model factory definitions for
| your application. Factories provide a convenient way to generate new
| model instances for testing / seeding your application's database.
|
*/

$factory->define(App\Usuario::class, function (Faker\Generator $faker) {
    return [
        'name' => $faker->name,
        'email' => $faker->unique()->safeEmail,
        'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret
        'remember_token' => str_random(10),
    ];
});

$factory->define(App\Carnet::class, function (Faker\Generator $faker) {
    return [
        'tipo' => $faker->?,
        'fechaExpedicion' => $faker->date,
    ];
});

el seeder individual
<?php

use Illuminate\Database\Seeder;
use Illuminate\Database\seeds\CarnetSeeder;
use Faker\Factory as Faker;
use Illuminate\Support\Facades\DB;

class CarnetSeeder extends Seeder
{
    /**
     * Corre el seed en la tabla carnet.
     *
     * @return void
     */
    public function run()

    {
          //creando el seed en la tabla con 20 ejemplos
        factory(App\Carnet::class, 20)->create();     


    }
}

the DatabaseSeeder

<?php

use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;

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

           $this->call(CarnetSeeder::class);  // ejemplo ejecucion seeder
                                              //$this->call(UsersTableSeeder::class);
       Model::reguard();     
    }
}
    
asked by FGB 22.04.2018 в 14:18
source

1 answer

0

In your CarnetSeeder class you must include the Carnet model:

<?php

    use Illuminate\Database\Seeder;
    use App\Carnet; 
    // No incluí los otros 'use' porqué veo que no los estás utilizando

    class CarnetSeeder extends Seeder
    {
        /**
         * Corre el seed en la tabla carnet.
         *
         * @return void
         */
        public function run()

        {
              //creando el seed en la tabla con 20 ejemplos
            factory(App\Carnet::class, 20)->create();     


        }
    }

And this must be found in the file database/seeds

    
answered by 24.04.2018 / 05:41
source