Php Laravel-如何访问WHERHADS子句中的属性?
我的实体如下:Php Laravel-如何访问WHERHADS子句中的属性?,php,database,laravel,eloquent,Php,Database,Laravel,Eloquent,我的实体如下: 请求类型 协议 流动 文件 文档类型 它们之间的关系如下: 协议属于请求者类型 协议有很多流 Flow有许多文档 文档属于文档类型 文档类型属于请求者类型 我需要的查询: 选择在第一个流中包含文档的所有协议(假设序列=1),其中包含其请求者类型所需的所有文档类型(实际上存储在协议) 例如,假设我有请求者类型为1的协议,并希望检查第一个流的文档中是否存在所有文档类型。要知道所有必需的文档类型,只需从协议请求者类型查询它 我从以下几点开始: $documentsTypesBy
- 请求类型
- 协议
- 流动
- 文件
- 文档类型
- 协议属于请求者类型
- 协议有很多流
- Flow有许多文档
- 文档属于文档类型
- 文档类型属于请求者类型
$documentsTypesByRequester = DocumentType::groupBy('requester_id')
->get();
$protocols = Protocol::whereHas('flows', function ($flows) use ($documentsTypesByRequester) {
return $flows->where('sequence', 1)
->whereHas('documents', function ($documents) use ($documentsTypesByRequester) {
// Select all documents that has the same
// requester id of protocol and all document types
// are present...
$requesterId = // How to access protocol requester id?
$documentTypes = $documentsTypesByRequester[$requesterId];
// ...
});
});
我需要用雄辩而不是收集来解决这个问题
似乎您正试图从
->whereHas()
中获得双重职责,我认为这只是将流
限制在那些有文档的流
。尝试从封装所有匹配逻辑的->where()
调用开始,然后使用一个简单的->whereHas('documents')
,看看它是如何工作的
比如说:
$documentsTypesByRequester = DocumentType::groupBy('requester_id')
->get();
$protocols = Protocol::whereHas('flows', function ($flows) use ($documentsTypesByRequester) {
return $flows->where('sequence', 1)
->where('documents', function ($query) use ($documentsTypesByRequester) {
// Put all of your logic for choosing a document here
// ...
})
->whereHas('documents');
});