Laravel在eloquant-relationship上检查多个参数
问题描述:
我有三个如下面结构的表。Laravel在eloquant-relationship上检查多个参数
表-1的AccountService
id | service_id| account_id|
----------------------------
1 | 1 | 8181 |
2 | 2 | 8181 |
3 | 3 | 8181 |
4 | 5 | 8181 |
表: - 服务
id | Name |
------------
1 | A |
2 | B |
3 | C |
4 | D |
表: - CompletedService
id | service_id | account_id
------------------------------
1 | 1 | 8181
2 | 1 | 8182
3 | 1 | 8181
4 | 2 | 8181
我现在的关系查询如下。
$acc = AccountService::with(['service' => function($q) {
}])->with(['completedServiceRest' => function($q) {
}])->where('account_id', $account_id)->get();
我如何在account_id和service_id的基础上获得关系部分的记录。 当前记录显示在completedSericeRest的关系中拥有account_id与AccountService表匹配的所有记录。
当这种关系发生再检查从已完成的服务并重的service_id和ACCOUNT_ID不仅ACCOUNT_ID。
低于模型关系。
public function completedServiceRest() {
return $this->hasMany('App\CompletedService', 'account_id', 'account_id');
}
目前得到ACCOUNT_ID的所有记录匹配不上这两个参数的service_id的基础和ACCOUNT_ID只影响ACCOUNT_ID。
在此先感谢。
答
这将是另一种结构,你的表“账户服务”的例子更容易(当然,完成后会默认为0):
id | service_id | account_id | completed |
------------------------------------------
1 | 1 | 8181 | 0 |
2 | 2 | 8181 | 1 |
你的主要关系是:
public function accountedServices()
{
return $this->hasMany('App\AccountService', 'account_id', 'account_id');
}
,并检查您完成的服务:
public function completedServiceRest()
{
return $services->accountedServices()->where('completed', 1)->get();
}
不,我不能改变我的表结构,因为有大量的OT她的领域在那里不仅完成了,还没有完成了0,1。所以这是行不通的。 TX –