Laravel 5.8
这些是我的桌子:
# Property
-id
-type[featured,standard]
-and others fields
# Meta
-id
-property_id
-and others fields
我已经设定了雄辩的关系(hasOne) 而且,我试图实现的是仅对类型为“ featured”的属性有条件地加载元关系。
如果您使用属性模型
运行查询Property::with('meta')->where('type','featured')->get();
如果您代表元模型运行查询。
在此查询中,您将获得property
类型具有featured
的元数据
Meta::whereHas('property',function($q){
$q->where('type','featured');
})
编辑:您的答案是正确的,但是如果我想同时获得所有特性和标准特性,并为它们添加元属性。
您想用Property
来获取所有meta
,然后此查询将返回Property
和meta
;
Property::with('meta')->get();
您需要为此使用紧急加载。
$properties= Property::all();;
$properties->where('type','featured')->load('meta');
dd($properties);