laravel - 如何显示每个类别的图像。谷歌云端硬盘
问题描述
我将类别图像存储在 Google Drive 中。数据库中的类别存储图像的名称,与 Google Drive 中的图像名称相同。通过这个名字,我得到了图片的 url 并将其显示在类别列中。但是,我想不出如何确保每个类别/子类别都有自己的图片。现在我所做的只是类别和子类别有不同的图片,但所有类别都有相同的图片,子类别也是如此。
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');
}
解决方案
啊,我现在明白你的问题了。您可以简单地在您的数组/集合中创建一个新字段,我称之为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>
在您的视图中访问图像 url,如:$item->img_url
或$subitem->img_url
,此值可能为 null,但这本身不应该是一个问题。
推荐阅读
- c++ - 有人可以解释一下这段代码如何能够反转字符串
- linux - 可以从内部暂停非特权 docker 容器吗?
- php - 如何在 WooCommerce 产品描述中显示所有图像
- python - scikit-learn 中使用标准 MAE 而不是 MSE 的随机森林回归慢约 150 倍
- android - 在 android 应用程序中禁用特定屏幕的语言 (LTR)
- django - 如何截断 wagtail 流场块的字符?
- javascript - JavaScript 将用户重定向到新页面
- javascript - 全屏菜单打开时如何禁用正文滚动
- javascript - 为什么我的 chrome 扩展程序图标在除 Chrome Web Store 之外的任何地方都可以使用?
- python - 如何使用模型表单在 django 中更新博客文章?