【问题标题】:Undefined variable in query builder查询生成器中的未定义变量
【发布时间】:2019-01-19 21:01:48
【问题描述】:

我有一个雄辩的查询问题。 当我运行查询时,我得到了未定义的变量:ccid

我已经通过函数通过了ccic

public function index($category)
{
    $currentuser = auth()->user();
    $ccid = $currentuser->clientcat_id;

    $products = DB::table('products')
        ->join('resomes', 'products.pricingcat_id', '=', 'resomes.pricingcat_id')
        ->join('users', function ($join) {
            $join->on('resomes.clientcat_id', '=', 'users.clientcat_id')
            //->where('users.clientcat_id', '=', 1);
            ->where(function($q) use($ccid){ 
                $q->where('users.clientcat_id', '=', $ccid);
            }
            );
        })
        ->select('products.*', 'resomes.discount', DB::raw('(products.price - (products.price * (resomes.discount/100))) as cPrice'))
        ->where('products.ccat_id', '=', $category)
        //->where(function($q) use($category){ $q->where('products.ccat_id', '=', $category);})
        ->orderBy('products.ccat_id', 'ASC')
        ->orderBy('products.price', 'ASC')
        ->paginate(config('pelma.products_list_pagination'));

    //print_r($products);
    return view('client.products.list', compact('products'));
}

有人有想法吗? 非常感谢

【问题讨论】:

  • ->join('users', function ($join) use($ccid) {...}) 必须将变量传入作用域。

标签: laravel eloquent laravel-5.7


【解决方案1】:

你没有将 $ccid 传递给

->join('users', function ($join)

你也必须使用 $ccid 这个函数。

【讨论】:

  • 哦!我检查了我的代码 30 次,但我没有看到它!非常感谢尼拉尔!你节省了我的时间!再次感谢
【解决方案2】:

你可以这样做

->join('users', function ($join) use ($ccid) {

这里有解释In PHP, what is a closure and why does it use the "use" identifier?

【讨论】:

    猜你喜欢
    • 2012-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-02
    • 1970-01-01
    • 2012-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多