gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 23:30:51 26 4
gpt4 key购买 nike

我的产品 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'));
}

这是我的产品 Controller 中的代码当我尝试运行此代码时,它在此处显示错误

最佳答案

那是因为map()不存在。

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

只是简称
$products = Product::where('id', $id)->first();

所以你只得到一个 Product模型回来。然后您尝试调用 map()在它上面,它只存在于 Collection如果结果包含多个数据集/模型,则返回的 s。但是你明确要求一个。所以你得到 Illuminate\Database\Eloquent\Model作为回应,然后尝试调用 map()在那。由于此方法不存在,Laravel 尝试遵循 Illuminate\Database\Query\Builder ,如果在那里没有找到那个方法,它会用上面提到的异常轰炸。

所以:只有一个 $products ,无需映射,直接使用即可。另外,如果你叫它 $product 会更清楚。而不是 $products .

关于php - 在 Laravel 5.4 中调用未定义的方法 Illuminate\Database\Query\Builder::map(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43979784/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com