How to Convert Laravel Collection Array to JSON

Sovary August 10, 2022 678
2 minutes read

Hello Artisan, this article will show you how to convert a collection to a JSON format in Laravel. We  will give you a simple example to turn collection array to JSON. We will assist you to convert collection data eloquent to json in Laravel which you can apply in Laravel 5, Laravel 6, Laravel 7, Laravel 8, Laravel 9. You will see below example laravel convert data to json example.

In some cases, you want to convert data to JSON format that manipulate from database as collection array. and there are many different ways to response data as JSON in Laravel, we will show you the different methods to turn your collection return from data to JSON format.

Let's check the following examples below:

Example #1 - Use JSON Response Helper

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\User;
  
class UserController extends Controller
{

    public function index()
    {
        $users= User::select('username','email','created_at')->latest()->get();
  
        dd(response()->json(['users' => $users]));
    }
}

Output #1

[
   {
      "username": 3,
      "email": "cambotutorial1@example.org",
      "created_at" : "2022-08-05T13:21:10.000000Z"
   },
   {
      "username": 2,
      "email": "cambotutorial2@example.org",
      "created_at" : "2022-08-11T13:21:10.000000Z"
   },
   {
      "username": 1,
      "email": "cambo2@example.org",
      "created_at" : "2022-08-11T13:21:10.000000Z"
   }
]

Example #2 - Use JSON Response

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\User;
  
class UserController extends Controller
{

    public function index()
    {
        $user= User::find(2);

        dd(\Response::json(['users' => $user]));
    }
}

Output #2

{
  "username": 2,
  "email": "cambotutorial1@example.org",
  "created_at" : "2022-08-11T13:21:10.000000Z"
}

 

Example #3 - Use toJson()

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\User;
  
class UserController extends Controller
{

    public function index()
    {
        $user = User::find(2);

        dd($user->toJson());
    }
}

Output #3

{
  "username": 2,
  "email": "cambotutorial1@example.org",
  "created_at" : "2022-08-11T13:21:10.000000Z"
}

Example #4 - Use json_encode()

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\User;
  
class UserController extends Controller
{

    public function index()
    {
        $users= User::select("id", "username")
                        ->latest()
                        ->take(3)
                        ->get();
  
        $users= json_encode($posts);
  
        dd($users);
    }
}

Output #4

[
   {
      "username": 3,
      "email": "cambotutorial1@example.org",
      "created_at" : "2022-08-05T13:21:10.000000Z"
   },
   {
      "username": 2,
      "email": "cambotutorial1@example.org",
      "created_at" : "2022-08-11T13:21:10.000000Z"
   }
]

Hope you find this article help you. Have a nice day!!!

You might also like...

Laravel  PHP  Laravel 9  Laravel Collections 
Author

As the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him