Laravel 4.2 检查 whereIn 子句中的数组是否为空

标签 laravel laravel-4 eloquent laravel-5

我使用 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/

相关文章:

php - 如何在 laravel Eloquent 中将 int 转换为字符串?

php - Blade 模板可能无法在 Laravel 4.1 中工作?

php - Laravel 即时更新电子邮件配置

php - WHERE 子句日期大于 24 小时 - Carbon/Laravel

mysql - 按字段最大值过滤重复项

php - Auth::guest() 为登录和注销用户返回 true

javascript - 快速并发的 ajax 调用

javascript - .reduce() 函数如何与 Javascript 中的 Math.max() 一起使用?

php - Laravel 5.2 使用 Eloquent 方法获取每行数据的 date_diff

php - 获取不带扩展名的文件名