LaravelNovaリソースタイトルでhtmlを使用する方法
Aug 21 2020
Laravel7とnova3.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' >";
}
私はlaravel7が{!!を使用していることを読みました。!!}(https://laravel.com/docs/7.x/blade#displaying-data )しかし、app / nova / some-resource.phpのリソースファイルで使用すると、phpでエラーが発生します。
リソースタイトルに国フィールドに基づいて画像を簡単に配置するにはどうすればよいですか?
-2020年8月23日更新
-> 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 = '';
回答
Pace Aug 23 2020 at 03:58
これを試して:
// for index
public static function label()
{
// input your logic to show label
return 'Your Label Index';
}
NoAd Sep 04 2020 at 03:38
私が見つけたフィールドで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(),
]),
また、リソースの$ titleに触れることなく、リソースの単一フィールドにフラグ画像と会社IDを含むHTMLがあります。