Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

osama_abdullah's avatar

Before rendering completely laravel nova set $this to empty instance of the model

When the nova application is booted $this is an empty instance of the model then it will have the expected values after that, the problem is anything you defined on the model will cause Call to a member function foo() on null error, here what I'm doing

class Score extends Model
{
    use HasFactory;

    public function student(): BelongsTo
    {
        return $this->belongsTo(Student::class);
    }

    // todo tobe tested
    public function department()
    {
        return $this->student ?
            $this->student->department() :
            dd($this);
    }
}

in the first call of the department() dd($this) prints an empty object of Score then it will be called again and $this will be assigned to the correct object that has values as expected.

How every outside Nove laravel will call the function once and load it with the correct object values

0 likes
0 replies

Please or to participate in this conversation.