phplaraveleloquenteloquent-relationship

During seeding, i faced this error "Attempt to read property "id" on null"


During seeding, i faced this error:

INFO Seeding database. Database\Seeders\RoleSeeder .................. RUNNING
ErrorException

Attempt to read property "id" on null

at C:\xampp\htdocs\cgpdocument\database\seeders\RoleSeeder.php:30

 26▕      [
 27▕        'id' => 'ff635a8f-4bb3-4d70-a3ed-c7749030696c',
 28▕        'isDeleted' => 0,
 29▕        'name' => 'Employee',
 30▕       'createdBy' => $user->id,
 31▕        'modifiedBy' => $user->id,
 32▕        'createdDate' => Carbon::now(),
 33▕        'modifiedDate' => Carbon::now()
 34▕      ],

1 C:\xampp\htdocs\cgpdocument\database\seeders\RoleSeeder.php:30

My migration is this:

public function up()
{
    Schema::create('roles', function (Blueprint $table) {
        $table->uuid('id')->primary();
        $table->boolean('isDeleted');
        $table->string('name')->nullable();
        $table->string('createdBy');
        $table->string('modifiedBy');
        $table->string('deletedBy')->nullable();
        $table->dateTime('createdDate');
        $table->dateTime('modifiedDate');
        $table->softDeletes()->nullable();
    });
}

Solution

  • Your $user object doesn't have an ID yet.

    Ensure that you have the $user saved to the database, so that you can safely get it's ID.