How to Automatically Return JSON for Laravel Eloquent

When working with APIs, you often need to return an Eloquent response in JSON format.

By default, Laravel transforms the response into JSON if you just return a Model or Eloquent Collection.

Example with Eloquent Collection

1// Controller:
2public function index()
3{
4 return User::all();
5}

Will return:

1[
2 {
3 "id": 1,
4 "name": "Prof. Marcos Ratke",
5 "email": "providenci.hane@example.org",
6 "email_verified_at": "2023-01-04T12:26:19.000000Z",
7 "created_at": "2023-01-04T12:26:20.000000Z",
8 "updated_at": "2023-01-04T12:26:20.000000Z"
9 },
10 {
11 "id": 2,
12 "name": "Sincere Casper",
13 "email": "jaylin33@example.com",
14 "email_verified_at": "2023-01-04T12:26:19.000000Z",
15 "created_at": "2023-01-04T12:26:20.000000Z",
16 "updated_at": "2023-01-04T12:26:20.000000Z"
17 },
18
19 // ... other users
20]

Example with Pagination

1// Controller:
2public function index()
3{
4 return User::paginate();
5}

Will return:

1{
2 "current_page": 1,
3 "data": [
4 {
5 "id": 1,
6 "name": "Prof. Marcos Ratke",
7 "email": "providenci.hane@example.org",
8 "email_verified_at": "2023-01-04T12:26:19.000000Z",
9 "created_at": "2023-01-04T12:26:20.000000Z",
10 "updated_at": "2023-01-04T12:26:20.000000Z"
11 },
12 {
13 "id": 2,
14 "name": "Sincere Casper",
15 "email": "jaylin33@example.com",
16 "email_verified_at": "2023-01-04T12:26:19.000000Z",
17 "created_at": "2023-01-04T12:26:20.000000Z",
18 "updated_at": "2023-01-04T12:26:20.000000Z"
19 },
20
21 // ... other users
22 ],
23 "first_page_url": "http://laravel.test/api/users?page=1",
24 "from": 1,
25 "last_page": 2,
26 "last_page_url": "http://laravel.test/api/users?page=2",
27 "links": [
28 {
29 "url": null,
30 "label": "« Previous",
31 "active": false
32 },
33 {
34 "url": "http://laravel.test/api/users?page=1",
35 "label": "1",
36 "active": true
37 },
38 {
39 "url": "http://laravel.test/api/users?page=2",
40 "label": "2",
41 "active": false
42 },
43 {
44 "url": "http://laravel.test/api/users?page=2",
45 "label": "Next »",
46 "active": false
47 }
48 ],
49 "next_page_url": "http://laravel.test/api/users?page=2",
50 "path": "http://laravel.test/api/users",
51 "per_page": 15,
52 "prev_page_url": null,
53 "to": 15,
54 "total": 20
55}

As you can see, the users are wrapped in data, and the main JSON contains more data about pages.


Example with a Single Model

1// Controller:
2public function show(User $user)
3{
4 return $user;
5}

Will return:

1{
2 "id": 1,
3 "name": "Prof. Marcos Ratke",
4 "email": "providenci.hane@example.org",
5 "email_verified_at": "2023-01-04T12:26:19.000000Z",
6 "created_at": "2023-01-04T12:26:20.000000Z",
7 "updated_at": "2023-01-04T12:26:20.000000Z"
8}

How Does It Work?

Quoting the official Laravel docs about Serialization:

Since models and collections are converted to JSON when cast to a string, you can return Eloquent objects directly from your application's routes or controllers. Laravel will automatically serialize your Eloquent models and collections to JSON when they are returned from routes or controllers:


Manually Return JSON

If you want to return the JSON from some other non-Eloquent structure, you can specify it with return response()->json(), passing array as a parameter:

1// Controller:
2public function update(UpdateUserRequest $request, User $user)
3{
4 $user->update($request->validated());
5 
6 return response()->json(['success' => true]);
7}

Will return:

1{
2 "success": true
3}

No comments or questions yet...

Like our articles?

Become a Premium Member for $129/year or $29/month

Recent Premium Tutorials