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

问题描述

我被困在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

我试图在资源中创建一个Text字段,因为它可以具有-> 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的目标的唯一途径:

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

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

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...