Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel-在查询生成器中访问模型值_Php_Mysql_Laravel_Eloquent - Fatal编程技术网

Php Laravel-在查询生成器中访问模型值

Php Laravel-在查询生成器中访问模型值,php,mysql,laravel,eloquent,Php,Mysql,Laravel,Eloquent,我有五个表:请求者、文档类型、协议、流和文档 关系 请求者有许多文档类型 请求者有许多协议 协议有很多流 Flow有许多文档 文档类型有许多文档 问题 为协议请求者选择具有所有文档类型的文档的具有第一个流的所有协议(假设序列=1) 换句话说,选择没有悬而未决的协议,这意味着在第一个流中包含文档的协议具有协议请求者所需的所有文档类型 解决方案 我最终得到了一个实际上不起作用的解决方案,因为我无法访问协议中的请求者id值,其中包含闭包: // All document types id's gr

我有五个表:请求者、文档类型、协议、流和文档

关系

  • 请求者有许多文档类型
  • 请求者有许多协议
  • 协议有很多流
  • Flow有许多文档
  • 文档类型有许多文档
问题

为协议请求者选择具有所有文档类型的文档的具有第一个流的所有协议(假设序列=1)

换句话说,选择没有悬而未决的协议,这意味着在第一个流中包含文档的协议具有协议请求者所需的所有文档类型

解决方案

我最终得到了一个实际上不起作用的解决方案,因为我无法访问协议
中的请求者id值,其中包含
闭包:

// All document types id's group by requester id's.
$documentTypesByRequester = DocumentType::all()
  ->groupBy('requester_id')
  ->map(function ($documentTypes, $requesterId) {
      return $documentTypes->pluck('id')
             ->toArray();
  })->toArray();
// Defined statically to reduce the complexity of question.
// $documentTypesByRequester = [
//    1 => [1, 2, 3],
//    2 => [4, 5]
// ];
// Here I have to select all protocols that have flows with
// documents having all document types of protocol requester.
$protocols = Protocol::whereHas('flows', function ($flows) use ($documentTypesByRequester) {
    // 
    // *---------------------------------------
    // * THIS DOESN'T WORK BUT IS WHAT I NEED!
    // *---------------------------------------
    // 
    // Access the requester id for current protocol.
    $requesterId = $flows->first()
        ->protocol
        ->requester_id;
    $documentTypesId = $documentTypesByRequester[$requesterId];

    return $flows->where('sequence', 1)
        ->whereHas('documents', function ($documents) use ($documentTypesId) {
            return $documents->whereIn('document_type_id', $documentTypesId);
        }, '=', count($documentTypesId));
})->get();
有一些方法可以访问此
中的模型值,其中有
闭包或存在另一种替代方法来帮助我解决此查询?

尝试以下方法:

$documentTypes = DocumentType::whereColumn('requester_id', 'protocol.requester_id');
$protocols = Protocol::whereHas('flows', function ($query) use ($documentTypes) {
    $query->where('sequence', 1)
        ->whereHas('documents', function ($query) use ($documentTypes) {
            $query->select(DB::raw('count(distinct document_type_id)'))
                ->whereIn('document_type_id', (clone $documentTypes)->select('id'));
        }, '=', DB::raw('('.(clone $documentTypes)->selectRaw('count(*)')->toSql().')'));
})->get();

嘿,您是否也可以在不静态设置
documentTypesByRequester
的地方共享代码?我想尝试一下这个查询,但是对于静态数据,它有点棘手,我可能会不必要地过于复杂化。@AndriusRimkus,这不是秘密。更新的问题。您可以发布迁移和关系吗?流是否可以有多个相同类型的文档(每个请求者)?@JonasStaudenmeir是!好把戏!但这不起作用,因为行
DB::raw('('(clone$documentTypes)->selectRaw('count(*)->toSql()。)
转换为sql字符串,而不是参数所期望的数字…您尝试过查询吗?看看Laravel API:
whereHas(string$relation,Closure$callback,string$operator='>=',int$count=1)
您仍然可以传递原始表达式。将
->get()
替换为
->toSql()
,然后查看结果。之前无法执行此选择并将其返回值传递给integer参数?