Laravel Nova-如何确定资源计算字段所在的视图(索引,详细信息,表单)? [英] Laravel Nova - How to determine the view (index, detail, form) you are in for a resource's computed field?
本文介绍了Laravel Nova-如何确定资源计算字段所在的视图(索引,详细信息,表单)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在查看索引视图时,与在查看资源的详细视图时,我想为计算字段返回不同的结果.
I would like to return a different result for a computed field when viewing the index view than when viewing the detail view of a resource.
基本上类似于以下的viewIs():
Basically something like viewIs() below:
Text::make('Preview', function () {
if($this->viewIs('index'){
return \small_preview($this->image);
}
return \large_preview($this->image);
})->asHtml(),
推荐答案
您可以检查请求的类别:
You can check the class of the request:
Text::make('Preview', function () use ($request) {
if ($request instanceof \Laravel\Nova\Http\Requests\ResourceDetailRequest) {
return \large_preview($this->image);
}
return \small_preview($this->image);
});
否则,您可以创建自己的viewIs函数:
Otherwise, you can create your own viewIs function:
// app/Nova/Resource.php
/**
* Check the current view.
*
* @param string $view
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @retrun bool
*/
public function viewIs($view, $request)
{
$class = '\Laravel\Nova\Http\Requests\\Resource'.ucfirst($view).'Request';
return $request instanceof $class;
}
然后您可以这样:
Text::make('Preview', function () use ($request) {
if ($this->viewIs('detail', $request) {
return \large_preview($this->image);
}
return \small_preview($this->image);
});
这篇关于Laravel Nova-如何确定资源计算字段所在的视图(索引,详细信息,表单)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文