Laravel中的JSON格式

az31mfrm  于 2022-12-14  发布在  其他
关注(0)|答案(4)|浏览(138)

假设我有三个模型和表,即Schools、Class和Students。School和Class是一对多关系,Class和Students也是一对多关系。现在我创建一个RESTFUL API,以JSON格式一次发送所有这些数据:

[{
        "1": {
            "school_id": 1,
            "school_name": "Havard",
            "Class": {
                "1": {
                    "class_id": 1,
                    "school_id": 1,
                    "class": "6",
                    "Student": {
                        "1": {
                            "Student_fname": "SomeName",
                            "Student_lname": "Last Name"
                        },
                        "2": {
                            "Student_fname": "Another",
                            "Student_lname": "AnotherLast"
                        }
                    },
                    "2": {
                        "class_id": 2,
                        "school_id": 28,
                        "class": "7",
                        "Student": {
                            "1": {
                                "Student_fname": "Anotherone",
                                "Student_lname": "Last"
                            },
                            "2": {
                                "Student_fname": "New",
                                "Student_lname": "Newer"
                            }
                        }
                    }
                }
            }
        }

    },
    {
        "2": {
            "school_id": 1,
            "school_name": "AnotherSchool",
            "Class": {
                "1": {
                    "class_id": 1,
                    "school_id": 1,
                    "class": "6",
                    "Student": {
                        "1": {
                            "Student_fname": "SomeName",
                            "Student_lname": "Last Name"
                        },
                        "2": {
                            "Student_fname": "Another",
                            "Student_lname": "AnotherLast"
                        }
                    },
                    "2": {
                        "class_id": 2,
                        "school_id": 28,
                        "class": "7",
                        "Student": {
                            "1": {
                                "Student_fname": "Anotherone",
                                "Student_lname": "Last"
                            },
                            "2": {
                                "Student_fname": "New",
                                "Student_lname": "Newer"
                            }
                        }
                    }
                }
            }
        }
    }
]

我如何实现这个格式?我的这个json格式正确吗?有没有更好的方法实现?

mtb9vblg

mtb9vblg2#

您可以使用https://github.com/thephpleague/fractalhttps://github.com/dingo/api之类的库,或者通过在控制器中创建转换器方法来实现(如果您喜欢,也可以创建类)
一些伪代码

<?php
class UserController extends Controller
{

    public function index(Request $request) {
        $users = Users::all(); 

        $response = []; 

        foreach ($users as $user) {
            $response[] = $this->transform($user); 
        }

    }

    public function transform(User $user) {
        return [
            'username' => $user->username,
            'age' => $user->age,
            'class' => [
                'name' => $user->school->name,
                'something' => $user->school->something
            ]
        ];
    }

}

我强烈建议在其他类中而不是在控制器本身中完成转换部分。

g6baxovj

g6baxovj3#

对于访问这个问题寻求Laravel中JSON格式化解决方案的人,我建议你使用JSON格式化程序Chrome上的扩展来测试你的API响应,而不是浪费时间去做一个简单的任务。这只是我个人的建议,你可以创建或使用插件来管理你代码上的JSON响应。

5n0oy7gb

5n0oy7gb4#

我想应该是这样的

[
"1": {
"school_id": 1,
    "school_name": "Havard",
    "Class": [
    "1": {
            "class_id": 1,
            "school_id": 1,
            "class": "6",
            "Student": [
                "1": {
                "Student_fname": "SomeName",
                    "Student_lname": "Last Name"
                }
            ]
         }
    ]
}
]

相关问题