如何获得正确的嵌套json格式?

时间:2019-06-26 12:21:59

标签: php json laravel

我想以嵌套的JSON格式显示联接查询的结果,但是我输入错误,将无法正常工作。我用两个表作为

category_type={category_type_id,category_type_name,category_icon};
main_category={main_category_id,category_type_id,category_name}

我想显示嵌套的JSON结果,但不希望得到解决方案。

控制器:

 $data= DB::table('table_main_category')
             ->join('table_category_type','table_main_category.category_type_id','=','table_category_type.category_type_id')
             ->select('table_category_type.*','table_main_category.*')
             ->get();


          return Response::json(array(
                    'success'     =>  '1',
                    'data'    =>   $data),
                    200
            );

json output:
{
    "success": "1",
    "data": [
        {
            "category_type_id": 2,
            "category_type": "Sports",
            "category_icon": "http://192.168.1.132:8000/images/category/game.svg",
            "main_category_id": 1,
            "category_name": "Popular Sports"
        },
        {
            "category_type_id": 2,
            "category_type": "Sports",
            "category_icon": "http://192.168.1.132:8000/images/category/game.svg",
            "main_category_id": 2,
            "category_name": "Team Sports"
        }

    ]
}

必需的json:

"success": "1",
    "data": [{
            "category_type_id": 2,
            "category_type": "Sports",
            "category_icon": "http://192.168.1.132:8000/images/category/game.svg",
            "main_category": {
                "main_category_id": 1,
                "category_name": "Popular Sports"
            }
        },
        {
            "category_type_id": 2,
            "category_type": "Sports",
            "category_icon": "http://192.168.1.132:8000/images/category/game.svg",
            "main_category": {
                "main_category_id": 2,
                "category_name": "Team Sports"
            }
        }
    ]

3 个答案:

答案 0 :(得分:0)

一种解决方法是在发送Response之前更新数据:

foreach ($data->toArray() as $elem) {
    $elem['main_category'] = [];
    $elem['main_category']['main_category_id'] = $elem['main_category_id'];
    unset($elem['main_category_id']);
    // ... and so on
}

答案 1 :(得分:0)

使用此代码重构数据:

 $data= DB::table('table_main_category')
             ->join('table_category_type','table_main_category.category_type_id','=','table_category_type.category_type_id')
             ->select('table_category_type.*','table_main_category.*')
             ->get();


          return Response::json(array(
                    'success'     =>  '1',
                    'data'    =>   $data.map( i => {  
                     const {main_category_id ,category_name , ...j } = i ;
                       return ({...j , main_category: {
                                         main_category_id,
                                       category_name }

                                         });

                                      }) ,
                    200
            );

答案 2 :(得分:0)

您可以使用雄辩的关系。首先创建您的模型。

use Illuminate\Database\Eloquent\Model;

class MainCategory extends Model
{
    public $table = 'table_main_category';
}

class CategoryType extends Model
{
    public $table = 'table_category_type';

    public function mainCategory()
    {
         return $this->belongsTo('App\MainCategory', 'category_type_id', 'category_type_id');
    }
}

然后,您可以在控制器中进行查询

$data = CategoryType::with('mainCategory')->get();

这将加载类别类型及其相应的主要类别

相关问题