2

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?

2
  • 1
    i think class(if classes are more than one) and student should be array of object(json). Commented Mar 17, 2018 at 4:31
  • Yes classes are more than one in a school. Could you show an example of a code to make it clear? Commented Mar 17, 2018 at 7:33

6 Answers 6

2

If like me, you want to format JSON string using Laravel collection please use the technique below:

$json = collect([
    'name' => 'Test',
    'description' => 'Another awesome laravel package',
    'license' => 'MIT'
  ])->toJson(JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);

echo $json;

The output generated would have the below format:

{
    "name": "Test",
    "description": "Another awesome laravel package",
    "license": "MIT"
}

The same technique can be applied to results of eloquent query. And writing JSON to files to storage.

Reference link

Sign up to request clarification or add additional context in comments.

Comments

1

You can use a library like https://github.com/thephpleague/fractal, https://github.com/dingo/api or do it by making transformer methods in your controllers(or classes if you prefer)

Some pseudo code

<?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
            ]
        ];
    }

}

I higly recommend doing the transform-part in other classes than in the controller itself.

Comments

1

For people visiting this question for a solution about JSON Formatting in Laravel, I recommend you use JSON formatter Extension on Chrome to test your API Responses instead of losing time for a simple task. It's just my personal recommendation, you can create or use a plugin to manage JSON responses on your code anyway.

Comments

0

Look into Laravel API Resources.

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

Comments

0

This works for me

public function index() {
    $ocorrencies = Ocorrency::all();
    return response()->json($ocorrencies, 200, [], JSON_PRETTY_PRINT);
}

Comments

-1

I think should be like this

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.