【发布时间】:2021-04-26 22:03:19
【问题描述】:
我正在尝试将 2 个变量传递给 View Composer,如下所示:
public function compose(View $view)
{
$catalog = Category::with('children')->where('parent_id', NULL)->get();
//
if(isset($catalog->img)){
// $cat = Category::
$contents = collect(Storage::disk('google')->listContents('sdfJSALSNldKdnslwk230jsd/', false));
$file = $contents
->where('type', '=', 'file')
->where('filename', '=', pathinfo($catalog->img, PATHINFO_FILENAME))
->where('extension', '=', pathinfo($catalog->img, PATHINFO_EXTENSION))
->first();
$catimg = isset($file['path'])?(Storage::disk('google')->exists($file['path'])?Storage::disk('google')->url($file['path']):NULL):NULL;
};
//
return $view->with(['catalog' => $catalog, 'catimg' => $catimg]);
}
但我收到错误消息:Undefined variable $catimg。
也许这不是传递第二个变量的正确方法?
而且,在顶部我检查图像是否存在于数据库中,但在我看来这是错误的方式,因为那里只返回父元素,我如何检查每个图像是否存在类别?大概需要跑foreach吧?
@foreach( $catalog as $item )
<li class='has-sub'><a href="#"><img class="catalogimg" src="@isset($item->img){{Storage::url($catimg)}}@else /img/categories/kitchen-utensils.png @endisset"><span class="cat-text">{{ $item->name }}</span></a>
<ul>
@foreach( $item->children as $subitem )
<li><a href='/{{ $item->url }}/{{ $subitem->url }}'><img class="catalogimg" src="@isset($subitem->img){{Storage::url($catimg)}}@else /img/categories/kitchen-utensils.png @endisset"><span class="cat-text">{{ $subitem->name }}</span></a></li>
@endforeach
</ul>
</li>
@endforeach
【问题讨论】: