首页 > 解决方案 > 如何显示每个类别的图像。谷歌云端硬盘

问题描述

我将类别图像存储在 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');
}

标签: laravelgoogle-drive-api

解决方案


啊,我现在明白你的问题了。您可以简单地在您的数组/集合中创建一个新字段,我称之为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,但这本身不应该是一个问题。


推荐阅读