skip to Main Content

Thanks for reading. I am trying to seed my database in laravel. When I ran "php artisan migrate:fresh –seed" command, only the database refreshing part was successful. I have already checked database user authentication, migration types and seeder types, and also models as well. I am simply just trying to seed one table, "chat_rooms". My app is laravel with Jetstream setup, so maybe that is the problem why? I am not sure. But here are my model, migration, and seeder. Please let me know what I can do.

ChatRoom.php model

namespace AppModels;

use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
use IlluminateDatabaseEloquentRelationsHasMany;

class ChatRoom extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
    ];

    public function messages(): HasMany
    {
        return $this->hasMany(ChatMessage::class);
    }
}
class ChatRoom extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
    ];

    public function messages(): HasMany
    {
        return $this->hasMany(ChatMessage::class);
    }
}

chat_rooms_table.php migration file

use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;

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

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

DatabaseSeeder.php

namespace DatabaseSeeders;

// use IlluminateDatabaseConsoleSeedsWithoutModelEvents;
use IlluminateDatabaseSeeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     */
    public function run(): void
    {
        $this->call([[
            ChatRoomSeeder::class,
        ]]);
    }
}

Error Output

  INFO  Seeding database.  


   TypeError 

  Illegal offset type in isset or empty

  at vendor/laravel/framework/src/Illuminate/Container/Container.php:1324
    1320▕      * @return string
    1321▕      */
    1322▕     public function getAlias($abstract)
    1323▕     {
  ➜ 1324▕         return isset($this->aliases[$abstract])
    1325▕                     ? $this->getAlias($this->aliases[$abstract])
    1326▕                     : $abstract;
    1327▕     }
    1328▕ 

      +3 vendor frames 

  4   database/seeders/DatabaseSeeder.php:16
      IlluminateDatabaseSeeder::call()
      +34 vendor frames 

  39  artisan:37
      IlluminateFoundationConsoleKernel::handle()

2

Answers


  1. Instead of passing an array with the seeder class name, you should directly provide the class name without enclosing it in another array.

    class DatabaseSeeder extends Seeder
    {
        /**
         * Seed the application's database.
         */
        public function run(): void
        {
            $this->call(ChatRoomSeeder::class);
        }
    }
    
    Login or Signup to reply.
  2. Your issue lies in the data passed in the call method of the DatabaseSeeder.php. It should not be an array inside an array. Per Doc, Calling Additional Seeder

    So your code block should look like

    class DatabaseSeeder extends Seeder
    {
        /**
         * Seed the application's database.
         */
        public function run(): void
        {
            $this->call([
                ChatRoomSeeder::class,
            ]);
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search