问题描述
这是我的书店数据库
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
推荐答案
您可以这样做:
Stock::with('Book.stock', 'Book.category')->get();
您可以在 with
语句中访问任意数量的嵌套关系.
You can access any number of nested relations within a with
statement.
相关问题:
阿明·萨姆(Armin Sam)的答案也应该是一个可行的选择.
Armin Sam's answer should also be a viable option.
这篇关于laravel orm:表格上的条件->相关表->相关表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!