Laravel 将变量传递给 whereha 查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38914821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:18:27  来源:igfitidea点击:

Laravel passing variable to wherehas query

phplaravellaravel-query-builder

提问by Manish Arora

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

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

在此处输入图片说明

回答by Shivam Chawla

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);

        });

回答by mishanon

If you want provide string in variable, use this code

如果您想在变量中提供字符串,请使用此代码

$catname = 'abc';

or declare abc constant

或声明 abc 常量

const abc = 123;