vendredi 8 septembre 2017

Laravel get a random user with where clause

i have this table schema. I have an id field. A name field and a friends id field. every user must have a maximum of 2 friends. When a new user is created and event is fired and a listener which listens to the created user event then adds a random friend to a newly created user.

 $randomfriend =  DB::table('users')->select('id')
                                   ->groupBy('friends_id')
                                   ->havingRAW('COUNT(*) < 2')
                                   ->inRandomOrder()->first(); 

it still returns users with maximum number of friends. Can someone help me with this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire