lundi 4 juin 2018

Laravel has on where function

I'm currently using this function to gather all of my users with a relationship

    $users = users::with(array('statusCurrent' => function($query)
    {
        $query->where('status.status', 'in');
        $query->orderBy('status.date', 'DESC');
    }));
    $users = $users->get();

This returns both of my users, and if status = 'in' then it returns the relationship aswell, but if status = 'out' it still returns the row, but with status_current = null.

Basically I want to ignore the user completely if the arguments inside the with query builder function are not true.

I have tried $candidates = $candidates->has('statusCurrent')->get(); to try and only get results where the relationship is not null, but it still returns users where the StatusCurrent relationship is null.

How do I do it so that foreach of the users, if whatever arguments I pass into the with(array('statusCurrent' => function(){}) are not true, it is ignored completely?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire