如何在 Laravel Nova 资源标题中使用 html

How to use html in Laravel Nova resource title

我坚持在 Laravel 7 & nova 3.8

中放置一个 html 输出

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

我尝试制作一个函数,将 html 图像放在索引页上某些资源的前面:

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

我读到 laravel 7 使用 {!! !!} ( https://laravel.com/docs/7.x/blade#displaying-data ) 但是,如果我在 app/nova/some-resource.php 的资源文件中使用它,php 会出错。

如何轻松地将基于国家/地区字段的图像放入资源标题中?

-- 更新 23.08.2020

我试图在我的资源中创建一个文本字段,因为它可以有 ->asHtml() 我在索引和详细视图中有一个漂亮的旗帜图像

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(), 
(...)
]};

标题中我改为:

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

结果是标题看起来像

''' '.$this->name // it looks like $this->country_flag = '';

试试这个:

// for index
public static function label()
{
    // input your logic to show label
    return 'Your Label Index';
}

我发现在野外唯一达到使用目标 html 的寺庙:

除了 belongsTo,我还使用 Stack 在文本和其他所需字段的数据中使用 html:

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(),
        ]),

我有一些 html 在资源的单个字段中带有标志图像和公司 ID,甚至没有触及资源 $title