Laravel 如何显示每个类别的图像。谷歌硬盘

Laravel 如何显示每个类别的图像。谷歌硬盘,laravel,google-drive-api,Laravel,Google Drive Api,我在谷歌硬盘中存储分类图像。数据库中的类别存储图像的名称,与Google Drive中的图像名称相同。通过这个名称,我获得图像的url并将其显示在category列中。但是,我想不出如何确保每个类别/子类别都有自己的图片。现在我所能做到的是,类别和子类别有不同的图片,但所有类别都有相同的图片,子类别也是如此 public function compose(View $view) { $catalog = Category::with('children')->where('

我在谷歌硬盘中存储分类图像。数据库中的类别存储图像的名称,与Google Drive中的图像名称相同。通过这个名称,我获得图像的url并将其显示在category列中。但是,我想不出如何确保每个类别/子类别都有自己的图片。现在我所能做到的是,类别和子类别有不同的图片,但所有类别都有相同的图片,子类别也是如此

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]);
  }
视图:


啊,我现在明白你的问题了。您只需在数组/集合中创建一个新字段,我称之为
img\u url
。将相应的URL保存到此字段中,然后访问它

公共函数组合(视图$View)
{
foreach($cat目录){
$catimg=null;//在此处将其定义为null
如果(isset($cat->img)){
$contents=collect(Storage::disk('google')->listContents('askldjfDSKLsOe2sdlKJF/',false));
$file=$contents
->其中('type','=','file')
->其中('filename','=',pathinfo($cat->img,pathinfo_filename))
->其中('extension','=',pathinfo($cat->img,pathinfo_extension))
->第一个();
$catimg=collect(isset($file['path'])?(Storage::disk('google')->exists($file['path'])?Storage::disk('google')->url($file['path']):NULL):NULL);
};
$cat['img_url']=$catimg;//创建一个名为img_url的新字段并赋值
foreach($cat->子对象作为$subcat){
$subcatimg=null;
如果(isset($subcat->img)){
$contents=collect(Storage::disk('google')->listContents('askldjfDSKLsOe2sdlKJF/',false));
$file=$contents
->其中('type','=','file')
->其中('filename','=',pathinfo($subcat->img,pathinfo_filename))
->其中('extension','=',pathinfo($subcat->img,pathinfo_extension))
->第一个();
$subcatimg=collect(isset($file['path'])?(Storage::disk('google')->exists($file['path'])?Storage::disk('google')->url($file['path']):NULL):NULL);
};
$subcat['img_url']=$subcat;
};
};
返回$view->with(['catalog'=>$catalog]);
}
视图:

    @foreach($商品目录)
    • @foreach($item->子项作为$subitem)
    • @endforeach
  • @endforeach

在您的视图中,访问图像url,如:
$item->img_url
$subitem->img_url
,此值可能为空,但这本身不应该是问题。

啊,我现在理解您的问题了。您只需在数组/集合中创建一个新字段,我称之为
img\u url
。将相应的URL保存到此字段中,然后访问它

公共函数组合(视图$View)
{
foreach($cat目录){
$catimg=null;//在此处将其定义为null
如果(isset($cat->img)){
$contents=collect(Storage::disk('google')->listContents('askldjfDSKLsOe2sdlKJF/',false));
$file=$contents
->其中('type','=','file')
->其中('filename','=',pathinfo($cat->img,pathinfo_filename))
->其中('extension','=',pathinfo($cat->img,pathinfo_extension))
->第一个();
$catimg=collect(isset($file['path'])?(Storage::disk('google')->exists($file['path'])?Storage::disk('google')->url($file['path']):NULL):NULL);
};
$cat['img_url']=$catimg;//创建一个名为img_url的新字段并赋值
foreach($cat->子对象作为$subcat){
$subcatimg=null;
如果(isset($subcat->img)){
$contents=collect(Storage::disk('google')->listContents('askldjfDSKLsOe2sdlKJF/',false));
$file=$contents
->其中('type','=','file')
->其中('filename','=',pathinfo($subcat->img,pathinfo_filename))
->其中('extension','=',pathinfo($subcat->img,pathinfo_extension))
->第一个();
$subcatimg=collect(isset($file['path'])?(Storage::disk('google')->exists($file['path'])?Storage::disk('google')->url($file['path']):NULL):NULL);
};
$subcat['img_url']=$subcat;
};
};
返回$view->with(['catalog'=>$catalog]);
}
视图:

    @foreach($商品目录)
    • @foreach($item->子项作为$subitem)
    • @endforeach
  • @endforeach

在您的视图中,访问图像url,如:
$item->img_url
$subitem->img_url
,此值可能为空,但这本身不应该是问题。

在您的数据库中,所有类别是否都有唯一的文件名,或者它们是否相同(可能是因为您对它们进行了种子设置)。因为我认为你的代码中没有一个问题不能让你得到正确的URL,每幅图片都有一个唯一的名称。重点是不同的,如何将这些URL的集合从控制器(在我的例子中是视图生成器)获取到图像,并将它们传输到视图中,以便每个类别都有自己的个人图像。在DB中,所有类别都有唯一的文件名还是相同的(可能是因为您对它们进行了种子设定)。因为我认为你的代码中没有一个问题不能让你得到正确的URL,每幅图片都有一个唯一的名称。重点是不同的,如何将这些URL的集合从控制器(在我的例子中是视图生成器)获取到图像,并将它们传输到视图,这样每个类别都有自己的个人图像。所以我不应该传递$catimg和$subcatimg变量?如果你不介意的话,你能更新这个问题并给出控制器和视图的完整代码吗?似乎我在某个地方感到困惑,并且做了一些错误的事情。@vcxbgfx我做了编辑,请告诉我是否还有什么问题
<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');
}