donglaoping9702 2017-05-15 12:37
浏览 174
已采纳

在laravel 5.4中调用未定义的方法Illuminate \ Database \ Query \ Builder :: map()

My product controller:

 public function category(Request $request, $name, $main){
          if($request->data){
            $explode_id = array_map('intval', explode(',', $request->data));
            $category_id = Category::where('name', $name)->value('id');
            if($category_id == "")
            {
              $all_categories_id = Category::pluck('id');
            }
            else
            {
            $all_categories_id = Category::where('parent_id', $category_id)->pluck('id');
            $all_categories_id->push($category_id);
            }
            $product_id = Product::where('name', 'like','%'.$main.'%')->whereIn('id', $explode_id)->pluck('id');
            $id = ProductCategory::whereIn('product_id', $product_id)->whereIn('category_id', $all_categories_id)->pluck('id');
            $products = Product::find($id); 
          }
          else{
              $category_id = Category::where('name', $name)->value('id');
              if($category_id == "")
              {
                $all_categories_id = Category::pluck('id');
              }
              else
              {
              $all_categories_id = Category::where('parent_id', $category_id)->pluck('id');
              $all_categories_id->push($category_id);
              }
              $product_id = Product::where('name', 'like','%'.$main.'%')->pluck('id');
              $id = ProductCategory::whereIn('product_id', $product_id)->whereIn('category_id', $all_categories_id)->pluck('id');
              $products = Product::find($id);
          }    

            //Categories Name in Sidebar
            $category = ProductCategory::whereIn('product_id', $id)->pluck('parent_id');
            $category_name = Category::whereIn('id', $category)->pluck('name');

            //Categories Name in dropdown
            $main_categories = Category::where('parent_id', '0')->pluck('name'); 

            $user = Auth::user();
            $directory = 'uploads/users/images/'.$user->id;
            $main_categories = Category::where('parent_id', '0')->pluck('name');
            if (is_dir($directory)) {
            $files = scandir ($directory);
            $img_file = $directory.'/'.$files[2];
            $user['front_img'] = $img_file;
            }
            $profile = $user['front_img'];
            //Product Image Mapping     
            $products->map(function ($product) {
            $directory = 'uploads/products/images/'.$product->id;
            $brand = Brand::select('name')->where('id', '=', $product->brand)->pluck('name');
            $brand_name = $brand->first(function($value, $key) {
              return $key == 'name';
            });
            if (is_dir($directory)) {
              $files = scandir ($directory);
              $img_file = $directory.'/'.$files[2];
              $product['front_img'] = $img_file;
              $product['brand'] = $brand_name;
              return $product;
            }
              return $product;
            });

          return view('pages/product', compact('main_categories', 'profile', 'products', 'name', 'category_name'));
        }

This is code in my product controller When i try to run this code it show the error here i used map() to map the products images and display it my view but why this error came ..Please any one help me..

  • 写回答

1条回答 默认 最新

  • dongtang1909 2017-05-15 13:11
    关注

    That's because map() does not exist.

    $products = Product::find($id);
    

    is just short for

    $products = Product::where('id', $id)->first();
    

    So you're only getting one single Product model back. Then you're trying to call map() on it, which only exists on Collections that are returned if the results contains multiple datasets / Models. But you're explicitly asking for a single one. So you're getting Illuminate\Database\Eloquent\Model in response and then try to call map() on that. Since this method does not exist, Laravel tries to defer to Illuminate\Database\Query\Builder, and if does not find that method there, it bombs out with the above mentioned Exception.

    So: There is only one $products, no need to map over it, just use it directly. Also, this would be even more clear if you called it $product instead of $products.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)