lundi 3 septembre 2018

How to replace query conditions in a loop in Laravel?

I have a user table with name, status and distance. I want to get the users under a particular distance and if the result is empty I want to check it again with an incremented value.

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

    $i = 10;

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

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

        $i = $i+5;
    };

In the first loop I got the query correctly as SELECT * from users where status = 1 and having distance <= 10. If the result is empty in the second loop I got the query as SELECT * from users where status = 1 and having distance <= 10 and having distance <= 15. I want to get the query as SELECT * from users where status = 1 and having distance <= 15. What change should I do for this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire