Laravel中的JSON格式

时间:2018-03-17 04:23:59

标签: json laravel rest api

假设我有三个模型和表格,即学校,班级和学生。 学校和班级是一对多的关系,班级和学生也是一对多的关系。现在我正在创建一个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格式是否正确?有没有更好的方法来实现它?

3 个答案:

答案 0 :(得分:2)

了解Laravel API资源。

Read this it will clear you Concepts

答案 1 :(得分:0)

您可以使用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
            ]
        ];
    }

}

我强烈建议在其他类中执行transform-part而不是在控制器本身中执行。

答案 2 :(得分:0)

我认为应该是这样的

[
"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"
                }
            ]
         }
    ]
}
]