简体   繁体   English

Laravel 将变量传递给 whereha 查询

[英]Laravel passing variable to wherehas query

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.我想将变量传递给 laravel 中的 wherehas 查询..但是得到未定义变量的错误,在方法中,如果有自然然后去哪里有自然等于 $catname... 行号。 4 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'));
}

在此处输入图片说明

To pass a variable to closure you have to use the use() function要将变量传递给闭包,您必须使用 use() 函数

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

        });

If you want provide string in variable, use this code如果要在变量中提供字符串,请使用此代码

$catname = 'abc';

or declare abc constant或声明 abc 常量

const abc = 123;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM