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

AntLusher's avatar

Lara Nova

Good morning all!

Please could you help with the following:

I have a JSON field called name with keys as follows: first, last, title.

On the index page, I wish to join all into one column: Mr Anthony Lusher.

At the moment I have this:

Text::make('Name', function () { return $this->name->first.' '.$this->name->last; })

The above doesn't work.

But I can get this to work:

Text::make('Name', 'name->title'),

Above show the title

Any help please

0 likes
15 replies
manelgavalda's avatar

I suggest you to use a custom accessor.

Firstly add this in your app model:

getFullNameAttribute() {
    return $this->name->first . ' ' . $this->name->last;
}

Then in your laravel nova model access it like this:

Text::make('FullName', 'full_name')
    ->exceptOnForms(),
1 like
musa11971's avatar

Can you try this?

Text::make('Name', function () {
    return $this->resource->name->first . ' ' . $this->resource->name->last;
})

If that works, you won't have to pollute your model šŸ˜„

1 like
AntLusher's avatar

Agh amazing - I did read your suggestion somewhere, but I assumed there was a 'Nova' way of doing it. I'll get right back to once I've tried.

Thanks @manelgavalda BRB

1 like
AntLusher's avatar

@musa11971 unfortunately that doesn't work. I get the following error:

Trying to get property of 'first' of non-object

manelgavalda's avatar

Is your json field correctly casted? Something like, in your model:

protected $casts = [
     "name" => "array"
];

Then access it like an array (using my way or @musa11971 way):

    return $this->name['first'] . ' ' . $this->name['last'];

If you are still getting this error make sure that the json field is not null or not correctly formatted.

AntLusher's avatar

@manelgavalda I've tried all of the following casts: json, collection, object, array. None of which works. Booo.

manelgavalda's avatar

If you are getting this error is probably because one of your db record doesnt have a correct name field.

If it's a collection you can use the optional operator for example, to avoid the error:

    return optional($this->name)->first . ' ' . optional($this->name)->last;
AntLusher's avatar

I think it's returning the 'name' value as a string, thus Nova providing an error. Nova won't be able to find any properties as it's been rendered has string.

So I assume I need to turn the 'name' string into an object?

I'm surprised Nova doesn't have any helpers for this use case.

@manelgavalda @musa11971

AntLusher's avatar

@manelgavalda hey sorry, I think you maybe confused. The field/column exists, and it's a JSON type field. I'm trying to get the values within the JSON.

AntLusher's avatar

@manelgavalda

So the Nova guy said:

Nova resources are not always resolved with an underlying Eloquent model instance. You need to check for the existence ofĀ $this->resourceĀ using theĀ optionalĀ helper or similar.

That's the same what you said.

manelgavalda's avatar

Hey, to make things easy I suggest you to just try with the first field, if it works we will add the last.

  • First make sure that the name field is casted to json with the casts attribute in your normal model.

  • Then do this directly in your nova Model:

Text::make('Name', function () {
    return optional($this->resource->name)->first;
})

This just works, if it doesnt, paste the code of your nova model, your migration and your normal model to see what's wrong.

AntLusher's avatar

Brilliant. Thanks so much. I will try first thing tomorrow.

Please or to participate in this conversation.