4

I'm stuck with putting a html output in Laravel 7 & nova 3.8

according to: https://nova.laravel.com/docs/3.0/search/global-search.html#title-subtitle-attributes

i try to make a function that put a html image in front of some resource on index pages:

public function title()
{
    return "<img width='20px' height='10px' src='./flags/".$this->country.".png' >";
}

I read that laravel 7 use {!! !!} ( https://laravel.com/docs/7.x/blade#displaying-data ) But if i use it in resource file in app/nova/some-resource.php php gives error.

How to easy put a image based on country field in resource title ?

-- update 23.08.2020

I tried to create a Text field in my resource as it can have ->asHtml() and i have a nice flag image on index and detail view

public function fields(Request $request)
{
    return [ 
(...)
Text::make('Country Flag',
            function () {
            return "<img width='20px' height='10px' src='http://fishmarket.nowakadmin.com/flags/".$this->country.".png' >";
        })->asHtml(), 
(...)
]};

and in title i changed to:

public function title()
{
    return $this->country_flag.' '.$this->name;
}

Result is that title looks like

''' '.$this->name // it looks like $this->country_flag = '';
NoAd
  • 141
  • 1
  • 14

2 Answers2

2

The only wat to achieve goal of use html in field as i found:

Instead of just belongsTo i used Stack to use html in Text and data from other needed fields:

Stack::make('Details', [
            Text::make('Country',
            function () {   
                $flaga = DB::table('companies')->where('id', $this->company_id)->first();
                return "<img width='20px' height='10px' src='/flags/".$flaga->country.".png'> ".$flaga->country;
            })->asHtml()
            ->sortable(),
            
            BelongsTo::make('Company')
                ->default(function ($request) {
                    return $request->user()->company_id;
                })
                ->sortable(),
        ]),

And i have some html with flag image and company id in single field on resource without even touching resource $title

NoAd
  • 141
  • 1
  • 14
1

try this:

// for index
public static function label()
{
    // input your logic to show label
    return 'Your Label Index';
}
PACE
  • 87
  • 1
  • 12
  • in witch files ? when i puuted it in my resource file app/nova/some_resource.php it give error: Error Using $this when not in object context (View: /var/www/html/my_app/vendor/laravel/nova/resources/views/layout.blade.php) – NoAd Aug 23 '20 at 08:50
  • yes its error, because `$this->country` not define in index resource. maybe add your logic to get this country – PACE Aug 23 '20 at 12:08
  • I adapt Your advice to my code. html putted into return of Your function is also html protected. Your code output change navigation bar displayed name to text given in that function I need it in title, not label. And also I need a html not a plain text. – NoAd Aug 23 '20 at 15:55