G
G
Governor2020-06-04 11:46:42
Laravel
Governor, 2020-06-04 11:46:42

How to get (number of records with condition) from linking model?

There are the following tables: 5ed8b1ff7218b251421457.pngand models Article, Tagand Comment.
When you click on a tag, you need to get a list of articles with a specific tag .

The fact is that the table articlesdoes not know about tags and data can only be obtained from a table article_tagthat does not have a model.
Temporarily solved the issue without models like this:

$countArticles = DB::select( 'SELECT COUNT(*) AS c FROM article_tag WHERE tag_id = ?;', [$tagID] )[0]->c;

But I want it more kosher. I'm waiting for advice.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question