laravel orm:表格上的条件->相关表相关表 [英] laravel orm : where condition on table -> related table -> related table

查看:42
本文介绍了laravel orm:表格上的条件->相关表相关表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的书店数据库

books : id ,title 
category : id,  title 

book_category : id , book_id,  category_id 
book_stock : id , book_id , quantity , price 

考虑到模型中定义的所有关系,我可以查询 book_stock 这样的事情

considering all the relations are defined in the model , i can query book_stock it goes something like this

Stock::with('Book')->get();

但是如果我想获取 category = 1

我可以在书上使用使用条件

i can use use condition on book

Stock::with('Book' , function($q){
   $q->where(['title'=>'abc']);
})->get();

但是我该如何过滤相关的书本表?基本上我想从 book_category 中获取 book_id ,其中 category_id = 1 ,然后使用这些ID最终过滤我的 books 得到 stock

but how can i filter related table of book ? basically i want to get book_id from book_category where category_id = 1 and then use those ids to filter my books finally get stock

ps:我不想使用查询生成器

ps : i don't want to use query builder

推荐答案

您可以这样做:

Stock::with('Book.stock', 'Book.category')->get();

您可以在 with 语句中访问任意数量的嵌套关系.

You can access any number of nested relations within a with statement.

相关问题:

Laravel嵌套关系

阿明·萨姆(Armin Sam)的答案也应该是一个可行的选择.

Armin Sam's answer should also be a viable option.

这篇关于laravel orm:表格上的条件->相关表相关表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆