jsonlaravel

JSON formatting in Laravel


Lets say I have three models and tables i.e. Schools, Class and students. The School and Class are in one to many relation and Class and students are also in one to many relationship. Now I am creating a RESTFUL API to send all these data at once in this JSON format:

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

How do I achieve this format? Is my this json format correct? Is there a better way to achieve it?


Solution

  • Look into Laravel API Resources.

    https://laravel.com/docs/5.6/eloquent-resources