【发布时间】:2019-01-30 06:38:34
【问题描述】:
所以,我在 Laravel 中工作,我有以下 API GET 路由,它从我的数据库返回一个包含文档及其类别的查询:
Route::get('documents', function() {
return DB::table('documents')
->join('documents_categories','documents_categories.documentCategoryID','=','documents.documentCategoryID')
->select('documentName','documentCategoryName')
->get(); });
返回按类别排序的所有文档:
{documentName: "Certificación de Licencia", documentCategoryName: "Aeronáutica Civil"}
{documentName: "Horas de Vuelo", documentCategoryName: "Aeronáutica Civil"}
{documentName: "Antecedente Penal", documentCategoryName: "Antecedentes"}
{documentName: "Traslado de Animales y Plantas", documentCategoryName: "Certificado de Sanidad Animal o Vegetal"}
{documentName: "Calidad y Apto para el Consumo Humano", documentCategoryName: "Certificados Comerciales"}
{documentName: "Certificado de Libre Venta", documentCategoryName: "Certificados Comerciales"}
{documentName: "Certificado de Origen", documentCategoryName: "Certificados Comerciales"}
{documentName: "Patente", documentCategoryName: "Certificados Comerciales"}
{documentName: "Registro de Producto", documentCategoryName: "Certificados Comerciales"}
{documentName: "Signos Distintivos", documentCategoryName: "Certificados Comerciales"}
现在,我想要实现的是将文档类别作为 JSON 键返回,并在数组中包含相应的文档。像这样的:
[
{
"Civiles":["document1","document2"],
"Antecedentes":["document1","document2"]
}
]
完成它的最佳方法是什么? 谢谢大家的帮助!
【问题讨论】:
-
Himad,你有什么尝试实现的?