dongmei425373 2017-05-17 21:01
浏览 20
已采纳

是否可以使用我的数据库结构使用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

  • 写回答

1条回答 默认 最新

  • duandongji2231 2017-05-17 21:16
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办