简体   繁体   English

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

[英]How to use html in Laravel Nova resource title

I'm stuck with putting a html output in Laravel 7 & nova 3.8我坚持将 html output 放入 Laravel 7 和 nova 3.8

according to: https://nova.laravel.com/docs/3.0/search/global-search.html#title-subtitle-attributes根据: 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:我尝试制作一个 function 将 html 图像放在索引页面上的某些资源前面:

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

I read that laravel 7 use {!!我读到 laravel 7 使用 {!! !!} ( 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. !!}( https://laravel.com/docs/7.x/blade#displaying-data )但是如果我在 app/nova/some-resource.php ZE1BFD762321E409CEE4AC0 的资源文件中使用它会给出错误。

How to easy put a image based on country field in resource title?如何在资源标题中轻松放置基于国家/地区字段的图像?

-- update 23.08.2020 -- 更新 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我试图在我的资源中创建一个文本字段,因为它可以有 ->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(), 
(...)
]};

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 = '';

The only wat to achieve goal of use html in field as i found:我发现,实现在现场使用 html 目标的唯一方法是:

Instead of just belongsTo i used Stack to use html in Text and data from other needed fields:我使用 Stack 来使用 html 在 Text 和来自其他所需字段的数据中,而不是只使用 belongsTo:

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我有一些 html 在资源的单个字段中带有标志图像和公司 ID,甚至没有触及资源 $title

try this:尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM