0
votes

My application returns an error and I need to know why. I made an AJAX request to get all products on he makes select on hot deals or special offers and ...

It returns data if I remove this query function. How I can pass this request selected box to this function?

Too few arguments to the function App\Http\Controllers\Website\AllProductController::App\Http\Controllers\Website\{closure}(), 1 passed in /...../vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php on line 226 and exactly 2 expected


public function ajax_category(Request $request)
    {
        if(isset($request->price) && isset($request->categories_id))
        {
        $product_category = $request->categories_id;

        // change the value from string to array.
        if (isset($request->selectedbox) && $request->selectedbox !='') {
            $pairs = $request->selectedbox;
            $newArray = explode(",", $pairs);
        }

        if (!empty($request->categories_id)) {
            $max = $request->max;
            $min = $request->min;
        } else {
            $min = product_model::where('pactive', 1)->select('MIN("price")')->first();
            $max = product_model::where('pactive', 1)->select('MAx("price")')->first();
        }

        // change the value from string to array.

        if (!empty($request->priceRange)) {

            $currentRange = $request->priceRange;
            $priceArray = explode(",", $currentRange);

            $firstPrice = $priceArray[0];
            $secondPrice = $priceArray[1];
        } else {
            $firstPrice = $min;
            $secondPrice = $max;
        }

        $products = product_model::where('category', $request->categories_id)
            ->whereBetween('price', [$firstPrice, $secondPrice]);
        if (isset($request->selectedbox) && $request->selectedbox !='') {
            $products = $products->where(function ($query,Request $request) {

                $pairs = $request->selectedbox;
                $newArray = explode(",", $pairs);

                $query->whereIn('poffertype',implode(',', $newArray))
                    ->orwhereIn('brand', implode(',', $newArray))
                    ->orwhereIn('brand_ar', implode(',', $newArray));
            });
        }
        $products = $products->with('p_images');

        if (isset($request->price)) {
            switch ($request->price) {
                case '1':
                    $products = $products->orderby('price', 'asc');
                    break;
                case '2':
                    $products = $products->orderby('price', 'desc');
                    break;
                case '3':
                    $products = $products->orderby('created_at', 'asc');
                    break;
                case '4':
                    $products = $products->orderby('created_at', 'desc');
                    break;
                default:
                    $products = $products->orderby('price', 'desc');
                    break;
            }
        }
            $products = $products->get();

1

1 Answers

2
votes

This part:

$products = $products->where(function ($query,Request $request) {

should be

$products = $products->where(function ($query) use ($request) {

The use keyword can be used for multiple purposes one of which is to inherit variables in closures.