如何显示每个类别的图像。 Google 开车
How to display an image of each category. Google Drive
我将类别图像存储在 Google 驱动器中。数据库中的类别存储的是图片的名称,与GoogleDrive中的图片名称相同。通过这个名称,我得到了图像的 url 并将其显示在类别列中。但是,我想不出如何确保每个 category/subcategory 都有自己的图片。现在我实现的是类别和子类别的图片不同,但是所有类别的图片都是一样的,子类别也是一样的。
public function compose(View $view)
{
$catalog = Category::with('children')->where('parent_id', '=', NULL)->get();
//
foreach ($catalog as $cat) {
if(isset($cat->img)){
$contents = collect(Storage::disk('google')->listContents('askldjfDSKLsOe2sdlKJF/', false));
$file = $contents
->where('type', '=', 'file')
->where('filename', '=', pathinfo($cat->img, PATHINFO_FILENAME))
->where('extension', '=', pathinfo($cat->img, PATHINFO_EXTENSION))
->first();
};
$catimg = collect(isset($cat->img)?(isset($file['path'])?(Storage::disk('google')->exists($file['path'])?Storage::disk('google')->url($file['path']):NULL):NULL):NULL);
foreach ($cat->children as $subcat) {
if(isset($subcat->img)){
$contents = collect(Storage::disk('google')->listContents('askldjfDSKLsOe2sdlKJF/', false));
$file = $contents
->where('type', '=', 'file')
->where('filename', '=', pathinfo($subcat->img, PATHINFO_FILENAME))
->where('extension', '=', pathinfo($subcat->img, PATHINFO_EXTENSION))
->first();
};
$subcatimg = collect(isset($subcat->img)?(isset($file['path'])?(Storage::disk('google')->exists($file['path'])?Storage::disk('google')->url($file['path']):NULL):NULL):NULL);
};
};
return $view->with(['catalog' => $catalog, 'catimg' => $catimg, 'subcatimg' => $subcatimg]);
}
查看:
<ul>
@foreach( $catalog as $item )
<li class='has-sub'><a href="#"><img class="catalogimg" src="@if($item->img != NULL && $catimg != NULL){{$catimg}}@else /img/categories/kitchen-utensils.png
@endif"><span class="cat-text">{{ $item->name }}</span></a>
<ul>
@foreach( $item->children as $subitem )
<li><a href='/{{ $subitem->url }}/{{ $subitem->url }}'><img class="catalogimg" src="@if($subitem->img != NULL && $subcatimg != NULL){{$subcatimg}}@else /img/categories/kitchen-utensils.png @endif"><span class="cat-text">{{ $subitem->name }}</span></a></li>
@endforeach
</ul>
</li>
@endforeach
</ul>
类别模型:
public function children()
{
return $this->hasMany(Category::class, 'parent_id');
}
啊,我明白你的问题了。您可以简单地在 array/collections 中创建一个新字段,我称之为 img_url
。将相应的 URL 保存到此字段中,稍后访问。
public function compose(View $view)
{
foreach ($catalog as $cat) {
$catimg = null; //define it here as null
if(isset($cat->img)){
$contents = collect(Storage::disk('google')->listContents('askldjfDSKLsOe2sdlKJF/', false));
$file = $contents
->where('type', '=', 'file')
->where('filename', '=', pathinfo($cat->img, PATHINFO_FILENAME))
->where('extension', '=', pathinfo($cat->img, PATHINFO_EXTENSION))
->first();
$catimg = collect(isset($file['path'])?(Storage::disk('google')->exists($file['path'])?Storage::disk('google')->url($file['path']):NULL):NULL);
};
$cat['img_url'] = $catimg; // create a new field called img_url and assign value
foreach ($cat->children as $subcat) {
$subcatimg = null;
if(isset($subcat->img)){
$contents = collect(Storage::disk('google')->listContents('askldjfDSKLsOe2sdlKJF/', false));
$file = $contents
->where('type', '=', 'file')
->where('filename', '=', pathinfo($subcat->img, PATHINFO_FILENAME))
->where('extension', '=', pathinfo($subcat->img, PATHINFO_EXTENSION))
->first();
$subcatimg = collect(isset($file['path'])?(Storage::disk('google')->exists($file['path'])?Storage::disk('google')->url($file['path']):NULL):NULL);
};
$subcat['img_url'] = $subcatimg;
};
};
return $view->with(['catalog' => $catalog]);
}
查看:
<ul>
@foreach( $catalog as $item )
<li class='has-sub'><a href="#"><img class="catalogimg" src="@if(!is_null($item->img_url)){{$item->img_url}}@else /img/categories/kitchen-utensils.png
@endif"><span class="cat-text">{{ $item->name }}</span></a>
<ul>
@foreach( $item->children as $subitem )
<li><a href='/{{ $subitem->url }}/{{ $subitem->url }}'><img class="catalogimg" src="@if(!is_null($subitem->img_url)){{$subitem->img_url}}@else /img/categories/kitchen-utensils.png @endif"><span class="cat-text">{{ $subitem->name }}</span></a></li>
@endforeach
</ul>
</li>
@endforeach
</ul>
在您的视图中访问图像网址,例如:$item->img_url
或 $subitem->img_url
,此值可能为 null,但这本身应该不是问题。
我将类别图像存储在 Google 驱动器中。数据库中的类别存储的是图片的名称,与GoogleDrive中的图片名称相同。通过这个名称,我得到了图像的 url 并将其显示在类别列中。但是,我想不出如何确保每个 category/subcategory 都有自己的图片。现在我实现的是类别和子类别的图片不同,但是所有类别的图片都是一样的,子类别也是一样的。
public function compose(View $view)
{
$catalog = Category::with('children')->where('parent_id', '=', NULL)->get();
//
foreach ($catalog as $cat) {
if(isset($cat->img)){
$contents = collect(Storage::disk('google')->listContents('askldjfDSKLsOe2sdlKJF/', false));
$file = $contents
->where('type', '=', 'file')
->where('filename', '=', pathinfo($cat->img, PATHINFO_FILENAME))
->where('extension', '=', pathinfo($cat->img, PATHINFO_EXTENSION))
->first();
};
$catimg = collect(isset($cat->img)?(isset($file['path'])?(Storage::disk('google')->exists($file['path'])?Storage::disk('google')->url($file['path']):NULL):NULL):NULL);
foreach ($cat->children as $subcat) {
if(isset($subcat->img)){
$contents = collect(Storage::disk('google')->listContents('askldjfDSKLsOe2sdlKJF/', false));
$file = $contents
->where('type', '=', 'file')
->where('filename', '=', pathinfo($subcat->img, PATHINFO_FILENAME))
->where('extension', '=', pathinfo($subcat->img, PATHINFO_EXTENSION))
->first();
};
$subcatimg = collect(isset($subcat->img)?(isset($file['path'])?(Storage::disk('google')->exists($file['path'])?Storage::disk('google')->url($file['path']):NULL):NULL):NULL);
};
};
return $view->with(['catalog' => $catalog, 'catimg' => $catimg, 'subcatimg' => $subcatimg]);
}
查看:
<ul>
@foreach( $catalog as $item )
<li class='has-sub'><a href="#"><img class="catalogimg" src="@if($item->img != NULL && $catimg != NULL){{$catimg}}@else /img/categories/kitchen-utensils.png
@endif"><span class="cat-text">{{ $item->name }}</span></a>
<ul>
@foreach( $item->children as $subitem )
<li><a href='/{{ $subitem->url }}/{{ $subitem->url }}'><img class="catalogimg" src="@if($subitem->img != NULL && $subcatimg != NULL){{$subcatimg}}@else /img/categories/kitchen-utensils.png @endif"><span class="cat-text">{{ $subitem->name }}</span></a></li>
@endforeach
</ul>
</li>
@endforeach
</ul>
类别模型:
public function children()
{
return $this->hasMany(Category::class, 'parent_id');
}
啊,我明白你的问题了。您可以简单地在 array/collections 中创建一个新字段,我称之为 img_url
。将相应的 URL 保存到此字段中,稍后访问。
public function compose(View $view)
{
foreach ($catalog as $cat) {
$catimg = null; //define it here as null
if(isset($cat->img)){
$contents = collect(Storage::disk('google')->listContents('askldjfDSKLsOe2sdlKJF/', false));
$file = $contents
->where('type', '=', 'file')
->where('filename', '=', pathinfo($cat->img, PATHINFO_FILENAME))
->where('extension', '=', pathinfo($cat->img, PATHINFO_EXTENSION))
->first();
$catimg = collect(isset($file['path'])?(Storage::disk('google')->exists($file['path'])?Storage::disk('google')->url($file['path']):NULL):NULL);
};
$cat['img_url'] = $catimg; // create a new field called img_url and assign value
foreach ($cat->children as $subcat) {
$subcatimg = null;
if(isset($subcat->img)){
$contents = collect(Storage::disk('google')->listContents('askldjfDSKLsOe2sdlKJF/', false));
$file = $contents
->where('type', '=', 'file')
->where('filename', '=', pathinfo($subcat->img, PATHINFO_FILENAME))
->where('extension', '=', pathinfo($subcat->img, PATHINFO_EXTENSION))
->first();
$subcatimg = collect(isset($file['path'])?(Storage::disk('google')->exists($file['path'])?Storage::disk('google')->url($file['path']):NULL):NULL);
};
$subcat['img_url'] = $subcatimg;
};
};
return $view->with(['catalog' => $catalog]);
}
查看:
<ul>
@foreach( $catalog as $item )
<li class='has-sub'><a href="#"><img class="catalogimg" src="@if(!is_null($item->img_url)){{$item->img_url}}@else /img/categories/kitchen-utensils.png
@endif"><span class="cat-text">{{ $item->name }}</span></a>
<ul>
@foreach( $item->children as $subitem )
<li><a href='/{{ $subitem->url }}/{{ $subitem->url }}'><img class="catalogimg" src="@if(!is_null($subitem->img_url)){{$subitem->img_url}}@else /img/categories/kitchen-utensils.png @endif"><span class="cat-text">{{ $subitem->name }}</span></a></li>
@endforeach
</ul>
</li>
@endforeach
</ul>
在您的视图中访问图像网址,例如:$item->img_url
或 $subitem->img_url
,此值可能为 null,但这本身应该不是问题。