【问题标题】:Laravel query using whereIn not working when insert array of data插入数据数组时使用whereIn的Laravel查询不起作用
【发布时间】:2019-07-11 14:11:19
【问题描述】:

我尝试使用 whereIn 获取促销数据,但查询出错。 在 whereIn 中,如果我让 whereIn('category_id', [123,2323]) 没有错误。

但是当我尝试从 request($data2 = $request->category;) 中获取数据时,我无法与 whereIn 一起使用

  $promotions = Promotion::with('product')->with('category')->whereHas(
        'category',
        function ($q) {
            $data2 = $request->category;

            $q->whereIn('category_id', $data2);
        })
        ->paginate(9);

【问题讨论】:

    标签: laravel


    【解决方案1】:

    你需要使用use($request)

    $promotions = Promotion::with('product')->with('category')->whereHas(
        'category',
        function ($q) use ($request){
            $data2 = $request->category;
    
            $q->whereIn('category_id', $data2);
        })
        ->paginate(9);
    

    $request->category 的样本数据。我以 json 格式返回数据并使用 javascript console.log() 显示

    【讨论】:

    • 分享你对$request->category的价值?
    • 我为问题中的 $request->category 添加了价值
    猜你喜欢
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-21
    • 1970-01-01
    • 1970-01-01
    • 2020-02-12
    相关资源
    最近更新 更多