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

Kar*_*iga 1 php laravel-5.4

我的产品控制器:

 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'));
        }
Run Code Online (Sandbox Code Playgroud)

这是我的产品控制器中的代码当我尝试运行此代码它显示错误在这里我使用map()来映射产品图像并显示我的视图,但为什么这个错误来了..请任何人帮助我..

Qua*_*unk 5

那是因为map()不存在.

$products = Product::find($id);
Run Code Online (Sandbox Code Playgroud)

只是为了

$products = Product::where('id', $id)->first();
Run Code Online (Sandbox Code Playgroud)

所以你只得到一个单一的Product模型.然后你试图调用map()它,Collection如果结果包含多个数据集/模型,它只存在于返回的s上.但你明确要求一个人.所以你得到Illuminate\Database\Eloquent\Model了回应,然后试着打电话map().由于这个方法不存在,Laravel会尝试推迟Illuminate\Database\Query\Builder,如果在那里找不到那个方法,它会用上面提到的异常进行轰炸.

所以:只有一个$products,不需要映射它,只需直接使用它.而且,如果你用它$product来代替它,这将更加清晰$products.