mercredi 21 décembre 2016

(Laravel) only retrieving users that have a relationship

In my site I have users and items. Users can create items. I want to get an array that has all users, where the users which have an item go first and the users which do not have an item go after.

So far I have done this:

$users = User::all();
foreach($users as $user) {
    if ($user->item) {
        $sortedUsers + $user;
    }
// now loop again and add users without relationship

This is pretty inefficient and I'm sure there's a much better way to do it.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire