Search code examples
laravellaravel-jetstream

Laravel Database Seeding Error TypeError Illegal offset type in isset or empty


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 App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

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 Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

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 Database\Seeders;

// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

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
      Illuminate\Database\Seeder::call()
      +34 vendor frames 

  39  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()

Solution

  • 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,
            ]);
        }
    }