[英]Laravel 5.4: Call to undefined method Illuminate/Database/Query/Builder::getRelated()
[英]Call to undefined method Illuminate\Database\Query\Builder::map() in 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'));
}
這是我的產品控制器中的代碼。當我嘗試運行此代碼時,它在此處顯示錯誤。我使用map()映射產品圖像並顯示其視圖,但是為什么會出現此錯誤..請任何人幫助我。
那是因為map()
不存在。
$products = Product::find($id);
簡而言之
$products = Product::where('id', $id)->first();
因此,您只能獲得一種Product
模型。 然后,您嘗試在其上調用map()
,該方法僅在結果包含多個數據集/模型的情況下返回的Collection
存在。 但是您明確要求一個。 因此,您得到了Illuminate\\Database\\Eloquent\\Model
作為響應,然后嘗試在其上調用map()
。 由於此方法不存在,因此Laravel嘗試遵照Illuminate\\Database\\Query\\Builder
,如果在那里找不到該方法,則它會拋出上述Exception異常。
因此:只有一個$products
,無需映射它,只需直接使用它即可。 而且,如果您將其命名為$product
而不是$products
,則將更加清楚。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.