【问题标题】:How to change data format returned from a MySQL query in PHP如何在 PHP 中更改从 MySQL 查询返回的数据格式
【发布时间】: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,你有什么尝试实现的?

标签: php mysql json laravel


【解决方案1】:

如果您使用 Laravel >=5.3,那么您的查询将返回 collection

您可以混合使用groupBy()map()pluck()

Route::get('documents', function () {
    return DB::table('documents')
        ->join('documents_categories', 'documents_categories.documentCategoryID', '=', 'documents.documentCategoryID')
        ->select('documentName', 'documentCategoryName')
        ->get()
        ->groupBy('documentCategoryName')
        ->map(function ($item) {
            return $item->pluck('documentName');
        });
});

如果您使用的是 Laravel 5.4,您可以更进一步,通过更改使用 Higher Order Messages

->map(function ($item) {
    return $item->pluck('documentName');
});

到:

->map->pluck('documentName');

【讨论】:

    【解决方案2】:

    查看关于集合类的 groupBy() 的 Laravel 文档。

    由于get()返回一个集合,你可以把这个方法链接到最后。

    Route::get('documents', function() {
        return DB::table('documents')
            ->join('documents_categories','documents_categories.documentCategoryID','=','documents.documentCategoryID')
            ->select('documentName','documentCategoryName')
            ->get()
            ->groupBy('documentCategoryName');
    });
    

    【讨论】:

      【解决方案3】:

      对不起,我弄错了你的问题。我的错。谢谢@Rafael

      编辑版本:

      你可以在$documentList上使用Laravel的collect方法

      $namelist = collect($documentList)->groupBy('documentCategoryName');
      

      它会返回这样的东西。不是json格式。它将是数组格式。

      $namelist = {
          category1: [
              {
                  documentName: "d1",
                  documentCategoryName: "category1"
              },
              {
                  documentName: "D2",
                  documentCategoryName: "category1"
              }
          ],
      }
      

      然后你就可以遍历这个数组了

      $res = array();
      foreach ($namelist as $key => $value){
          array_push($res[$key], $value['documentname']);
      }
      

      然后返回$res。这将是您想要的输出。

      如果您正在寻找类似的东西,请告知。

      【讨论】:

      • 这不是他真正问的@Amku91。据他说,响应已经以 json 格式出现。
      • 感谢您的建议。现在看看他在找什么。
      • 很高兴为您提供帮助。 :)
      猜你喜欢
      • 2018-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-25
      • 2021-08-20
      • 1970-01-01
      相关资源
      最近更新 更多