1

I'm running a few seeders during a migration after creating a table. Here's my migration file create_institutions_table

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

class CreateInstitutionsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('institutions', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('code');
            $table->timestamps();
            $table->softDeletes();
        });

        $seeder = new InstitutionsSeeder();
        $seeder->run();

        $seeder2 = new UsersSeeder();
        $seeder2->run();

        Schema::table('users', function (Blueprint $table) {
            $table->foreign('institution_id')->references('id')->on('institutions');
        });
    }

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

here's the InstitutionsSeeder

use Illuminate\Database\Seeder;

class InstitutionsSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::table('institutions')->insert([
            'name' => 'Institution One',
            'code' => 'I1',
        ]);

    }
}

here's the UsersSeeder

use Illuminate\Database\Seeder;

class UsersSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::table('users')->insert([
            'first_name' => 'Admin',
            'last_name' => 'Istrator',
            'email' => 'admin@example.com',
            'institution_id' => '1',
            'password' => '$2y$10$/wYQaaaaaaagrtyh64gbdt4yuhr32l4VmFHI.sINMR/9LXsj1MTy',
        ]);
    }
}

As far as I can tell there's no real difference between the seeders, but the migration fails when trying to instanciate the UsersSeeder class while the InstitutionsSeeder works fine. This is the exception I'm getting from the php artisan migrate:fresh command:

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

  at H:\code\MyProject\database\migrations\2019_06_17_224612_create_institutions_table.php:27
    23| 
    24|                 $seeder = new InstitutionsSeeder();
    25|                 $seeder->run();
    26| 
  > 27|                 $seeder2 = new UsersSeeder();
    28|                 $seeder2->run();
    29| 
    30|                 Schema::table('users', function (Blueprint $table) {
    31|                         $table->foreign('institution_id')->references('id')->on('institutions');

  Exception trace:

  1   CreateInstitutionsTable::up()
      H:\code\MyProject\vendor\laravel\framework\src\Illuminate\Database\Migrations\Migrator.php:379

  2   Illuminate\Database\Migrations\Migrator::Illuminate\Database\Migrations\{closure}()
      H:\code\MyProject\vendor\laravel\framework\src\Illuminate\Database\Migrations\Migrator.php:388

  Please use the argument -v to see more details.

Why won't the UsersSeeder work?

solidau
  • 4,021
  • 3
  • 24
  • 45
  • Is there a specific reason why you are running your database seeds within your migrations? If not, I would strongly encourage you to run these in Database seeder instead. This will also likely fix your issue... – Ben Carey Jun 19 '19 at 22:56

2 Answers2

2

Two possible solutions:

  1. Check the namespace of your class

  2. Run composer dump-autoload: composer dump-autoload (You can read the docs here)

Mateus Junges
  • 2,559
  • 1
  • 12
  • 24
  • `composer dump-autoload` did the trick. I'm getting back into the PHP ecosystem, so this is invaluable information, thanks! I realize now that it became out of sync because I copied the file instead of using the respective `php artisan make` commands. I understand that while you've outlined the 'by the book' way for seeding data as indicated in the Laravel docs, I am unable to do that for reasons that are beyond the scope of this question/answer, so I will probably edit that part out. Thanks again! – solidau Jun 19 '19 at 23:31
  • Happy to help you! – Mateus Junges Jun 19 '19 at 23:35
0

Each time you create a new seeder run composer dump-autoload command. After that just run the seeder using php artisan db:seed command. Hope this will work !

Jithesh Jose
  • 1,781
  • 1
  • 6
  • 17