'Laravel Nova displayUsing() and title() conflict
displayUsing() writes over the title() on the Nova resource. I use displayUsing() to display text on the index page a certain way. I use title() to display text a certain way when searching the records on a BelongsTo field. I want the location field on the index page to only show city, state (Chicago,IL) however, when searching for that location on a BelongsTo, I would like it to show 123 State St Chicago,IL 60601. Whats happening in this case is even though the title() has the full address, it only shows whats in the displayUsing(), which is city, state.
Laravel Version: 8.81.0 Nova Version: 3.30.0 PHP Version: 7.4.6
Solution 1:[1]
The way I got around this was to add:
public static $with = ['origin'];
to the nova resource and use a computed field to customize the display:
Text::make('Origin', function () {
return $this->origin->city . ', ' . $this->origin->state;
})->onlyOnIndex(),
So now Origin can be displayed one way when searching for it on a BelongTo field, but it can be displayed differently on the Index page.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mcornille |
