How to Convert Collection to JSON in Laravel?

By Hardik Savani April 16, 2024 Category : Laravel

Hi Dev,

This tutorial is focused on how to convert collection to json in laravel. This post will give you a simple example of laravel convert object to json. I would like to show you laravel collection to json example. we will help you to give an example of laravel eloquent object to json. Let's see below example laravel convert data to json example.

You can use this example with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version as well.

Sometimes, we are getting data from the database and you need to convert eloquent data into JSON then how you will do this? don't worry, there are many ways to convert the collection to JSON in laravel. we will use toJson() and json_encode() method to convert objects array to JSON in laravel.

so let's see below one by one example:

Example 1: get() with toJson()

PostController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Post;

class PostController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

$posts = Post::select("id", "title", "body")

->latest()

->get()

->toJson();

dd($posts);

}

}

Output:

[

{

"id":40,

"title":"Post title 1",

"body":"Post body"

},

{

"id":39,

"title":"Post title 2",

"body":"Post body"

},

{

"id":38,

"title":"Post title 3",

"body":"Post body"

}

]

Example 2: find() with toJson()

PostController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Post;

class PostController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

$post = Post::find(40)->toJson();

dd($post);

}

}

Output:

{

"id":40,

"title":"Post title 1",

"slug":null,

"body":"Post body",

"created_at":"2022-08-05",

"updated_at":"2022-08-05T13:21:10.000000Z",

"status":1

}

Example 3: json_encode()

PostController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Post;

class PostController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

$posts = Post::select("id", "title", "body")

->latest()

->take(5)

->get();

$posts = json_encode($posts);

dd($posts);

}

}

Output:

[

{

"id":40,

"title":"Post title 1",

"body":"Post body"

},

{

"id":39,

"title":"Post title 2",

"body":"Post body"

},

{

"id":38,

"title":"Post title 3",

"body":"Post body"

}

]

Example 4: Custom Collection using toJson()

PostController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PostController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

$posts = collect([

['id' => 1, 'title' => 'Title One', 'body' => 'Body One'],

['id' => 2, 'title' => 'Title Two', 'body' => 'Body Two'],

]);

$posts = $posts->toJson();

dd($posts);

}

}

Output:

[

{

"id":1,

"title":"Title One",

"body":"Body One"

},

{

"id":2,

"title":"Title Two",

"body":"Body Two"

}

]

Example 5: JSON Response

PostController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Post;

class PostController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

$posts = Post::select("id", "title", "body")

->latest()

->get();

return response()->json(['posts' => $posts]);

}

}

Output:

{"posts":[{"id":40,"title":"Post title 1","body":"Post body"},{"id":39,"title":"Post title 2","body":"Post body"},{"id":38,"title":"Post title 3","body":"Post body"},{"id":37,"title":"Post title 4","body":"Post body"},{"id":36,"title":"Post title 5","body":"Post body"}]}

I hope it can help you...

Tags :
Shares