Laravel:订单 hasManyThrough

Laravel: Order hasManyThrough

我很难理解 laravel 中的“hasManyThrough”概念。 我得到了类似于这些表的东西:

project
- id

project_document
- id
- project_id

project_document_order
- id
- project_document_id

在项目模型中我有这些关系:

public function projectDocuments(){
    return $this->hasMany(
        ProjectDocuments::class,
        'project_id'
    );
}

public function projectDocumentOrder(){
    return $this->hasManyThrough(
        ProjectDocumentOrder::class,
        ProjectDocuments::class,
        'project_id',
        'project_document_id'
    );
}



Project::where( 'id', $pid )->with( 'projectDocuments', 'projectDocumentOrder' )->firstOrFail();

这导致一个项目具有多个 projectDocuments 和多个 projectDocumentOrder。

结果:

#original: array:14 [▼]
"id" => 1
"uuid" => [UUID]
"author" => 1
"project_name" => [NAME]

#relations: array:2 [▼]
"projectDocuments" => Illuminate\Database\Eloquent\Collection {#1352 ▼
    #items: array:5 [▼
        0 => App\Models\projectDocuments {#1368 ▼
            #original: array:10 [▼
                "id" => 1
                "user_id" => 471
                "project_id" => 1
        }
        1 => App\Models\projectDocuments {#1368 ▼
            #original: array:10 [▼
                "id" => 2
                "user_id" => 252
                "project_id" => 1
        }
        2 => App\Models\projectDocuments {#1368 ▼
            #original: array:10 [▼
                "id" => 3
                "user_id" => 252
                "project_id" => 1
        }
        3 => App\Models\projectDocuments {#1368 ▼
            #original: array:10 [▼
                "id" => 4
                "user_id" => 471
                "project_id" => 1
        }
        4 => App\Models\projectDocuments {#1368 ▼
            #original: array:10 [▼
                "id" => 5
                "user_id" => 252
                "project_id" => 1
        }
    ]
}
"projectDocumentOrder" => Illuminate\Database\Eloquent\Collection {#1367 ▼
  #items: array:5 [▼
        0 => App\Models\projectDocumentOrder {#1368 ▼
            #original: array:10 [▼
                "id" => 1
                "project_documents_id" => 1
                "order" => 1
        }
        1 => App\Models\projectDocumentOrder {#1368 ▼
            #original: array:10 [▼
                "id" => 2
                "project_documents_id" => 1
                "order" => 2
        }
        2 => App\Models\projectDocumentOrder {#1368 ▼
            #original: array:10 [▼
                "id" => 3
                "project_documents_id" => 1
                "order" => 3
        }
        3 => App\Models\projectDocumentOrder {#1368 ▼
            #original: array:10 [▼
                "id" => 4
                "project_documents_id" => 1
                "order" => 5
        }
        4 => App\Models\projectDocumentOrder {#1368 ▼
            #original: array:10 [▼
                "id" => 5
                "project_documents_id" => 1
                "order" => 4
        }
    ]
}

现在我的问题:我将如何根据 projectDocumentOrder 中的顺序列来订购 projectDocuments?

首先我认为您只需要 with: 'projectDocumentOrder',因为 laravel 要获得 projectDocumentOrder,将需要加入 projectDocuments。

其次你可以这样做:

Project::where( 'id', $pid )->with(['projectDocuments'=>function($query){
   $query->leftJoin('table_name','local_key','operator','foreign_key');
   $query->orderBy('table_name.field','ASC|DESC');
)
}])->firstOrFail();

我找到了适合我的解决方案。

$project->projectDocumentOrder->sortBy( 'order' )->pluck('projectDocuments')->toJson()

此解决方案确实需要使用 projectDocuments 函数将 ProjectDocumentOrder 链接到 ProjectDocumentOrder 模型中的 ProjectDocuments。