【问题标题】:Laravel passing variable to wherehas queryLaravel 将变量传递给 wherehas 查询
【发布时间】:2016-12-19 06:46:35
【问题描述】:

我想将变量传递给 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'));
}

【问题讨论】:

  • $catname = abc; 应该是$catname = 'abc';
  • 它的动态变量
  • 它的未定义变量
  • 这应该可以正常工作。向我们展示完整的代码
  • 签出上面的代码...我已经更新了代码..

标签: php laravel laravel-query-builder


【解决方案1】:

要将变量传递给闭包,您必须使用 use() 函数

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

        });

【讨论】:

  • 你还没有在此声明下这样做$sorting = $request->sorting ? $request->sorting : '';
  • 您在代码中调用了两次 whereHas。您第一次使用了 use() 函数,而不是第二次
  • 为什么我需要三元运算符,因为我从函数本身传递它
【解决方案2】:

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

$catname = 'abc';

或声明 abc 常量

const abc = 123;

【讨论】:

  • 它的动态变量
  • 我的意思是你的代码 $catname = abc;是错的。你声明 abc 常量了吗?
猜你喜欢
  • 2017-01-30
  • 2015-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-16
  • 2017-11-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多