13

I want to pass variable to wherehas query in laravel.. but getting an error of undefined variable, In method, if has nature then go where has natures equal to $catname... in line no. 4

public function Products($catname,Request $request)     //Product Category Pages
{
    $natures = Nature::where('nature_slug', '=', $catname)
                    ->first();
    if($natures)
    {   //Where Clause Based On Products Nature
        //dd($catname);
        $maxproductscost = Product::selectRaw('MAX(ABS(price)) AS HighestPrice')
                                ->whereHas('natures', function($q) use ($catname)
                                    {
                                        $q->where('nature_slug', '=', $catname);
                                    })
                                ->first();
        $maxproductscost = ceiling($maxproductscost->HighestPrice, 100);
        /*End - GEt Maximum cost of product*/
        if($request->range){
            $range = $request->range;
            $pieces = explode(" ", $range);
            $rangestart = $pieces['1'];
            $rangeend = $pieces['4'];
        }
        $firstslidervalue = $request->range ? $rangestart : 0;
        $secondslidervalue = $request->range ? $rangeend : $maxproductscost;
        $sorting = $request->sorting ? $request->sorting : '';

        $products = Product::whereHas('natures', function($q)
            {
                $q->where('nature_slug', '=', $catname);

            });
        
    }
    else
    {
        //Where Clause Based On Products Nature is General
        /*GEt Maximum cost of product*/
        $maxproductscost = Product::selectRaw('MAX(ABS(price)) AS HighestPrice')
                                ->where('ptype', '=', $catname)
                                ->whereHas('natures', function($q)
                                    {
                                        $q->where('nature_slug', '=', 'general');
                                    })
                                ->first();
        $maxproductscost = ceiling($maxproductscost->HighestPrice, 100);
        /*End - GEt Maximum cost of product*/
        if($request->range){
            $range = $request->range;
            $pieces = explode(" ", $range);
            $rangestart = $pieces['1'];
            $rangeend = $pieces['4'];
        }
        $firstslidervalue = $request->range ? $rangestart : 0;
        $secondslidervalue = $request->range ? $rangeend : $maxproductscost;
        $sorting = $request->sorting ? $request->sorting : '';

        $products = Product::where('ptype', '=', $catname)
                        ->whereHas('natures', function($q)
            {
                $q->where('nature_slug', '=', 'general');

            });
    }

    if($request->range){
        $products->whereBetween('price', [$rangestart, $rangeend]);
    }
    if($sorting)
    {
        if($sorting == 'low')
        {
            $products->orderByRaw('(ABS(stock) > 0) desc, (case when ABS(stock) > 0 then ABS(price) end) asc, (case when ABS(stock) = 0 then ABS(price) end) asc ');
        } else
        {
            $products->orderByRaw('(ABS(stock) > 0) desc, (case when ABS(stock) > 0 then ABS(price) end) DESC, (case when ABS(stock) = 0 then ABS(price) end) DESC ');
        }
    }
    else
    {
        $products->orderByRaw('(ABS(stock) > 0) desc, (case when ABS(stock) > 0 then id end) DESC, (case when ABS(stock) = 0 then id end) DESC ');
    }
    
        
    $products = $products->paginate(12);

    $user = Auth::user();               
    return view('products',compact('user','catname','products','maxproductscost','firstslidervalue','secondslidervalue','sorting'));
}

enter image description here

5
  • $catname = abc; should be $catname = 'abc'; Commented Aug 12, 2016 at 9:46
  • its dynamic variable Commented Aug 12, 2016 at 9:52
  • its undefined variable Commented Aug 12, 2016 at 10:05
  • This should work fine. Show us the full code Commented Aug 12, 2016 at 10:08
  • checkout the above code...i had updated the code.. Commented Aug 12, 2016 at 10:13

2 Answers 2

44

To pass a variable to closure you have to use the use() function

$products = Product::whereHas('natures', function($q) use($catname)
        {
            $q->where('nature_slug', '=', $catname);

        });
Sign up to request clarification or add additional context in comments.

3 Comments

You havent done this under this statement $sorting = $request->sorting ? $request->sorting : '';
You have the whereHas thing called twice in the code. You have used the use() function 1st time and not the 2nd time
why do i need ternary operator as i am passing it from function itself
0

If you want provide string in variable, use this code

$catname = 'abc';

or declare abc constant

const abc = 123;

2 Comments

its dynamic variable
I mean you code $catname = abc; is wrong. did you declared abc constant?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.