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..