如何在Laravel中循环中替换查询条件?

问题描述 投票:0回答:1

我有一张名字,状态和距离的user表。我想让用户在特定的distance下,如果结果为空,我想用增量值再次检查它。

    $user = User::where('status',1);

    $i = 10;

    while($i < 50)
    {
        $user->having('distance', '<=', $i);

        if($user->get()->count())
            break;

        $i = $i+5;
    };

在第一个循环中,我得到了正确的查询SELECT * from users where status = 1 and having distance <= 10。如果结果在第二个循环中为空,则我将查询作为SELECT * from users where status = 1 and having distance <= 10 and having distance <= 15。我想将查询作为SELECT * from users where status = 1 and having distance <= 15。我应该为此做些什么改变?

sql laravel laravel-5 eloquent laravel-5.6
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.