Home Laravel How to Convert Collection to JSON in Laravel?

How to Convert Collection to JSON in Laravel?

by Shahriar Sagor

Today now in this tutorial i will focused on how to convert the collection to json in laravel. So in this post i will give you a very simple example of laravel convert object to json. Here I would like to share with you laravel collection to json example. So here i will help you about an example of laravel eloquent object to json. Let’s start and see the below example laravel convert data to json example.

We can also use this example with laravel 6, laravel 7, laravel 8 and laravel 9 any version .

Sometimes, we are getting the data from the database and we have to convert an eloquent data into JSON format then how we will do this? don’t worry, there are many ways to convert a collection to JSON in laravel. Here i use toJson() and json_encode() method to convert any objects array to JSON in laravel.

So let’s see the 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);
    }
}

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);
    }
}

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);
    }
}

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);
    }
}

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

Read Also: php artisan serve not working in Laravel

Thanks for read. I hope it help you. For more you can follow us on facebook

close

You may also like