【发布时间】:2021-03-06 19:31:25
【问题描述】:
这是我的人际关系:
User hasMany licences
Licence belongsTo UserLicence hasMany attestations
Attestation belongsTo LicenceAttestation hasMany vehicles
Vehicle belongsTo Attestation
例子:
用户id | name1 | John Doe
许可证id | user_id | type1 | 1 | A2 | 1 | B
证明id | licence_id | date1 | 1 | 2020-12-052 | 1 | 2021-02-143 | 2 | 2021-03-02
车辆id | attestation_id | type1 | 1 | car2 | 1 | boat3 | 2 | motorcycle4 | 2 | truck5 | 3 | plane6 | 3 | train
这就是我目前所拥有的:
User::whereHas('licences', function($query) use($search) {
if (!empty($search['licence_type'])) {
$query->where('type', $search['licence_type']);
}
$query->whereHas('attestations', function($query) use($search) {
if (isset($search['vehicles'])) {
foreach ($search['vehicles'] as $vehicle) {
$query->whereHas('vehicles', function($query) use($search, $vehicle) {
$query->where('type', $vehicle);
});
}
}
});
})->get();
现在在我的过滤器中,如果我搜索“汽车”,则会检索到用户。但是,如果我搜索“汽车和摩托车”或“汽车和飞机”,则不会检索到用户。
我希望查询搜索给定用户的所有证明。
我该怎么做?
【问题讨论】:
-
car AND motorcycle或car AND plane具有不同的 attestation_id。您可能需要orWhere而不是 where。