How can I fetch all users who has at least 5 cars?


#1

Hi, I wanna ask about lucid relationship
For example I have users and cars table. They have many to many relationship. How can I fetch all users who has at least 5 cars?
Thanks


#2

hello @mukhsin

try to use the whereHas method https://adonisjs.com/docs/4.1/relationships#_wherehas

I am thinking you already have the relationships defined, you might try something like this:

const user = await User.query().whereHas('cars','>=',5).fetch()

I did not tested this


#3

Never thought it :smile:
Ok, thanks. I’ll try


#4

It works
Somewhy I didn’t though it :man_facepalming:
Thanks


#5

you’re welcome :slight_smile: