Retrieve data in junction table

Hi all bro, please help me how to retrieve list of users who like this item in Yii2

I have 3 table

User: id, user_name

Item: id, item_name

Like: id, user_id, item_id

Thanks all for help,

If you used the Gii Model generator :




//List of User[]

$users = $item->users;

//OR

$users = $item->getUsers()->all();



Dear Timmy78,

I use the Like table to save all users like this item, and I can’t see item->users in Gii Model generator. Can you help me?

Thanks you much,

If you define the foreign keys in the database first, Gii generates the relations in the model for you.

Thanks Timmy78 and Hauke29, I created fk and generate models again, it’s OK.