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

时间:2017-01-05 10:39:33

标签: laravel laravel-4 eloquent laravel-5.2

我使用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); 子句的问题,如果数组为空则不会返回结果:

tf.Variable

我如何在whereIn中使用我的数组检查它是否为空或为空

2 个答案:

答案 0 :(得分:3)

尝试使用条件:

$articles = DB::table('articles')->orderBy('created_at','desc');

if (!empty($idsUsers)) {
    $articles = $articles->whereIn('user_id', $idsUsers);
}

$articles = $articles->paginate(10);

答案 1 :(得分:0)

你可以试试这个

if ([UIApplication sharedApplication].applicationState ==  UIApplicationStateBackground) {
    // Root view controller loaded while in background, so doing only background stuff
    [self justDoBackgroundStuffWithoutUI];
} else {
    // Root view controller loaded normally, so loading normal UI
    [self initializeUIAndChildControllers];
}

希望这会有所帮助

相关问题