约束渴望的关系 [英] Constraining eager loaded relationship
问题描述
我发现with
函数的异常行为使关系超载.我有Product
和Deal
关系,例如Product
belongsTo()
Deal
(通过deals
表中的product_id
).现在,当我尝试出售所有产品时:
I found a very bizarre behavior of with
function for overloading relationships. I have Product
and Deal
relationships, such that Product
belongsTo()
Deal
(through product_id
in deals
table). Now, when I try to get all products on sale:
Product::with(['deal' => function($query) {
$query->whereDate('ends_at', '>', Carbon::now()->toDateTimeString());
}])->get()
这将返回所有产品的集合,即使deals
表中没有没有记录并且所有产品具有deal_id
设置为NULL
.同时,如您所料,Product::has('deal')->get()
返回一个空集合.
this returns a collection of all products, even though there are no records in deals
table and all products have deal_id
set to NULL
. At the same time Product::has('deal')->get()
returns an empty collection, as you would expect.
我最初是在尝试获取五种随机产品以及Deal
和Image
关系的时候发现这个问题的:
I initially discovered this problem while trying to fetch five random products on sale together with Deal
and Image
relationships:
Product::with(['deal' => function ($query) {
$query->whereDate('ends_at', '>', // promo still active
Carbon::now()->toDateTimeString());
},
'images' => function ($query) {
$query->where('featured', true); // image featured on homepage
}])
->where('status', 'IN_STOCK') // 'In Stock'
->whereNull('deleted_at') // wasn't soft-deleted
->orderByRaw('RAND()')
->take(5)->get())
这将产生一个集合,其中所有Product
中有5个随机Product
.我尝试使用query->whereNotNull('ends_at')->whereDate('ends_at' ..... );
,但得到了相同的结果.
This yields a collection with 5 random Product
s out of all Product
s. I tried with query->whereNotNull('ends_at')->whereDate('ends_at' ..... );
but got same results.
我在做什么错了?
推荐答案
您对此概念的理解完全是错误的.
Your understanding of the concept is completely wrong here.
如果您说的是产品 belongsTo()
交易,那么假设交易 hasMany()
产品.
If you are saying that a Product belongsTo()
Deal, then lets assume that a Deal hasMany()
Products.
这是特价表
deals
id | name | ends_at | blah | blah
products
id | deal_id | name | blah | blah
因此,基本上Product::with('deal')
应该会向您返回所有其交易均已热切加载的产品.但是Deal::with('products')
将返回一个空集合,因为其中没有产品包含有效的deal_id
.
So basically, the Product::with('deal')
should return you all products with their Deals being Eager loaded. But Deal::with('products')
will return you an empty collection, since no products have a valid deal_id
in it.
请注意,由于产品只能belongTo
单个交易,因此在执行Product::with('deal')
查询.但是,当您执行Deal::with('products')
时,您必然会得到一个集合.
It is important to note that, since Product can only belongTo
a single Deal, you will always get the Deal Model rather than a collection when you perform Product::with('deal')
query. But when you perform Deal::with('products')
you are bound to get a collection.
所以基本上,当你说
这将返回所有产品的集合,即使在交易表中没有记录并且所有产品的deal_id都设置为NULL.
This returns a collection of all products, even though there are no records in deals table and all products have deal_id set to NULL.
这很明显 ....,因为此处的查询是针对产品"而不是交易"进行的.如果要在ends_at > Carbon::now()
所在的位置找到交易,则必须这样做.
It is pretty obvious.... because the query here is being done on Products and not Deal. If you are trying to find the Deal where ends_at > Carbon::now()
, you'll have to do this.
Deal::with('product')->where('ends_at', '>', Carbon::now()->toDateTimeString())
这篇关于约束渴望的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!