2017-05-15 53 views
0

我的產品控制器:調用未定義的方法照亮數據庫查詢生成器::在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')); 
     } 

這是我的產品控制器代碼時,我嘗試運行此代碼,它顯示錯誤在這裏我用地圖()來映射產品圖像,並顯示它我的看法,但爲什麼這個錯誤來了..請任何一個幫助我..

+1

Dude .... **請考慮一些嚴重的重構**(也許一些PSR-2指南)。這很明顯,例如,你試圖運行'map()'的'$ products'是一個'Model'而不是'Collection'(你可以期待給出這個變量名......)。 – Quasdunk

回答

3

這是因爲map()不存在。

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

只是短暫的

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

所以,你只得到一個單一Product模型回來。然後,您嘗試對其調用map(),該結果只存在於Collections上,如果結果包含多個數據集/模型,則返回該結果。但是你明確要求一個。所以你得到Illuminate\Database\Eloquent\Model作爲迴應,然後嘗試撥打map()。由於這種方法不存在,Laravel試圖推遲到Illuminate\Database\Query\Builder,如果在那裏沒有找到那個方法,它會用上面提到的異常炸彈。

所以:只有一個$products,不需要映射它,直接使用它。此外,如果您將其稱爲$product而不是$products,這將更加清晰。

相關問題