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

hehe's avatar
Level 6

New syntax accessor does not work in Nova custom tools ?

I have defined a few accessors in my Journey model using the following syntax recommended since Laravel@9

protected function <attribute_name>(): Attribue
{
...
}

Accessors works well but in my custom tool.

In the <custom_tool_dir>/routes/inertia.php I just pass my model instance to inertia.

Route::get('/{journey}', function (Journey $journey) {
    return inertia('Tool', compact(['journey']));
});

I get a the following error message.

Call to undefined method App\Models\Journey::get<attribute_name>Attribute()

I guess it would work using the old syntax... Any advice on this ?

0 likes
6 replies
LaryAI's avatar
Level 58

It seems like you're encountering an issue where the new accessor syntax introduced in Laravel 9 isn't being recognized when you're passing your model instance to an Inertia response. This could be due to a serialization issue where the accessor methods are not being called when the model is converted to a JSON representation for the Inertia response.

Here's a potential solution to ensure that your accessor is included in the JSON representation:

First, make sure that your accessor method is correctly defined in your Journey model. For example, if you have an accessor for an attribute named example, it should look like this:

use Illuminate\Database\Eloquent\Casts\Attribute;

protected function example(): Attribute
{
    return new Attribute(
        get: fn ($value) => // Your get logic here,
        set: fn ($value) => // Your set logic here, if necessary
    );
}

Next, you need to ensure that the accessor is included in the array representation of your model. You can do this by adding the accessor to the $appends property of your model:

protected $appends = ['example'];

This will ensure that when your model is serialized to JSON, the example attribute is included in the JSON representation.

Now, when you pass your model instance to Inertia, the accessor should be included:

Route::get('/{journey}', function (Journey $journey) {
    return inertia('Tool', compact('journey'));
});

Make sure that your route model binding is correctly resolving the Journey instance and that the example attribute is accessible on the Journey model instance.

If you're still encountering issues, you may want to manually add the accessor value to the array you're passing to Inertia like so:

Route::get('/{journey}', function (Journey $journey) {
    $journeyArray = $journey->toArray();
    $journeyArray['example'] = $journey->example; // Manually add the accessor

    return inertia('Tool', ['journey' => $journeyArray]);
});

This manually appends the accessor to the array before passing it to Inertia, which can help if automatic serialization isn't including the accessor for some reason.

If none of these solutions work, please ensure that you're using the latest version of Laravel and Inertia, as there may have been bug fixes or updates that address this issue.

hehe's avatar
Level 6

IT seems that it is not coming from Nova as when a do a $model->toArray() I get the same error. So it would come from the serialization process.

Robstar's avatar

As per the documentation, that link directly answers your original question.

Actually just noticed the LaraCasts AI auto reply has cited the same example.

Unsure what additional help can be provided :/

hehe's avatar
Level 6

I tried what said the documentation.

I tried to set $appends, $casts with, without custom cast and still have the same error.

Please or to participate in this conversation.