Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Get articles by tag (many to many)

Hi! I have made model connections (many to many) and I can easily get the tags by article

Tables:

articles (id, name)

tags (id, name)

articlestags (article_id, tag_id)

categories (id, name)

articlescategories (article_id, category_id)

  1. How to get articles by tag_id and category_id (the orm way)? for example get articles with tag_id = 1 and category_id = 2
  2. How to get articles with multiple tags and categories? for example get articles that have tag_id = 1, tag_id = 2, category_id = 3, category_id = 4


113.9k
Accepted
answer
edited Oct '15
  1. You have to use modelsManager or static ::query method on articletags and join articles where tag_id = 1 and join articlescategories and add where category_id = 2.

  2. Same, just use modelsManager or static ::query method, WHERE tag_id =1 OR tag_id =2 OR category_id = 3 OR category_id = 4.

In the "orm way" its gonna be hard and you will make so many select operations, with modelsManager or static ::query method you will do only one.