user6594805
user6594805

Reputation: 1

error with Attempt to read property "taqseet" on null while it not null laravel

I am trying to fix all issues but I get Attempt to read property "taqseet" on null.

Student Model

public function taqseet()
{
    return $this->hasMany(Taqseet::class, 'stud_prog_id', 'id');
}

CourseController

the error point to this line

$link = StudentProgramLink::with(['sanad','taqseet', 'student'])->find($StudProgLink);

When I dd($link), there's taqseet data:

#relations: array:3 [▼
    "sanad" => Illuminate\Database\Eloquent\Collection {#2490 ▶}
    "taqseet" => Illuminate\Database\Eloquent\Collection {#2486 ▼
      #items: array:4 [▼
        0 => App\Models\Taqseet {#2531 ▶}
        1 => App\Models\Taqseet {#2529 ▶}
        2 => App\Models\Taqseet {#2528 ▶}
        3 => App\Models\Taqseet {#2527 ▶}
      ]
      #escapeWhenCastingToString: false
    }
    "student" => App\Models\Student {#2547 ▶}

I can't find a solution for this.

I tried with withDefault function but it does not work with HasMany relation, it only works with BelongsTo.

Upvotes: 0

Views: 54

Answers (1)

gokul
gokul

Reputation: 59

$taqseet = $link->taqseet;

This will get the taqseet data:

Upvotes: 0

Related Questions