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

christopher's avatar

Nova tries to access a field when creating while onlyOnIndex

So i have a status field in my index and while creating a BelongsTo field.

Text::make('Status', function () {
                return view('partials.status', [
                    'name' => $this->status->name,
                    'color' => $this->status->color
                ])->render();
            })->asHtml()->onlyOnIndex(),
BelongsTo::make('Status')->hideFromIndex(),

But while creating an entry, Nova says "Trying to get property 'name' of non-object" which is the line 'name' => $this->status->name, which i don't understand because i set it to onlyOnIndex(), why is Nova taking it for creation?

0 likes
5 replies
bobbybouwmann's avatar
Level 88

Well, sadly that is how Nova works. I also had some trouble with Nova when working on accessors and related data in lists. For some reason, Nova is always calling all code.

Anyway, you can work around it like so

Text::make('Status', function () {
    if (!$this->status instanceOf \App\Status::class) {
        return null;
    }

    return view('partials.status', [
        'name' => $this->status->name,
        'color' => $this->status->color
    ])->render();
})->asHtml()->onlyOnIndex(),

This way it should work properly in my experience ;)

christopher's avatar

Thank you @bobbybouwmann But here i am getting Unexpected class constant reference on \App\Status::class. Also tried just Status and import the model and then i am getting syntax error, unexpected 'class' (T_CLASS), expecting variable (T_VARIABLE) or '$'

bobbybouwmann's avatar

Aah sorry, wrong syntax! It should be

if (!$this->status instanceOf \App\Status) {
    return null
}

// Or

use App\Status;

if (!$this->status instanceOf Status) {
    return null
}
christopher's avatar

My fault - I had another field which requires the same code for another field ^^ Thanks so much!

Please or to participate in this conversation.