我使用 elequent
进行查询,我的 whereIn
子句有问题,如果数组为空,则不会返回结果:
$idsUsers = empty(json_decode($data["idsUsers"])) ? null : json_decode($data["idsUsers"]);
$articles = DB::table('articles')
->whereIn('user_id',$idsUsers)
->orderBy('created_at','desc')
->paginate(10);
我如何在 whereIn 方法中使用我的数组来检查它是否不为空或 null
最佳答案
尝试使用条件:
$articles = DB::table('articles')->orderBy('created_at','desc');
if (!empty($idsUsers)) {
$articles = $articles->whereIn('user_id', $idsUsers);
}
$articles = $articles->paginate(10);
关于Laravel 4.2 检查 whereIn 子句中的数组是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41482824/