是否可以使用我的数据库结构使用Laravel或Sql执行此查询?

是否可以使用我的数据库结构使用Laravel或Sql执行此查询?

问题描述:

I have a database with a table articles and a table category.

My table articles have some fields. And one that is category_id and a another orientation.

Categories, in my design, are arranged by orientation. Here is an example enter image description here

So, I would like to get all category BUT these categories must belong to the right orientation. I want list of all category who have articles with 'web' orientation, by example.

I do not know if it's possible with this architecture and if you understand me.

Any help is welcome

UPDATE : adding the schemas

enter image description here

我有一个数据库,其中包含表 articles code>和一个表 category 代码>。 p>

我的表文章 code>有一些字段。 还有一个是 category_id code>和另一个 orientation code>。 p>

在我的设计中,类别按方向排列。 这是一个例子 p>

因此,我希望得到所有类别,但这些类别必须属于正确的方向。 我希望所有类别的列表都包含具有“网络”方向的文章 strong>,例如。 p>

我不知道这种架构是否可行以及您是否理解 我欢迎任何帮助 p>

更新:添加模式 p>

p> div>

If you have your models and relations set up, I believe this should do what you want:

$categories = Category::whereHas('articles', function($query) {
   $query->where('orientation', 'web')
})->get()

From the Laravel documentation:

If you need even more power, you may use the whereHas and orWhereHas methods to put "where" conditions on your has queries. These methods allow you to add customized constraints to a relationship constraint, such as checking the content of a comment