ItSolutionStuff.com

How to Convert JSON to Collection in Laravel?

By Hardik Savani • February 1, 2024
Laravel

In this post, i will show you how to convert json to collection in laravel. i will give you very simple controller code for laravel convert json to collection.

In this example, i will create simple controller method and take json variable. Then i will convert json to collection using "collect()" and json_decode() method. so, let's see the simple example with output code:

Example:

You can see the following controller code:

app/Http/Controllers/HomeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller

{

/**

* Create a new controller instance.

*

* @return void

*/

public function index()

{

/* Your JSON string */

$jsonString = '{

"name": "Hardik Savani",

"age": 30,

"city": "Rajkot"

}';

/* Convert JSON string to a collection */

$dataCollection = collect(json_decode($jsonString, true));

dd($dataCollection);

}

}

Output:

Illuminate\Support\Collection {#1881 ▼

#items: array:3 [▼

"name" => "Hardik Savani"

"age" => 30

"city" => "Rajkot"

]

}

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Collection Sort By Date Example

Read Now →

Laravel Collection Remove Item by Value Example

Read Now →

How to Remove Null and Empty Values from Laravel Collection?

Read Now →

Laravel Collection Remove Last Item Example

Read Now →

Laravel Collection Remove First Item Example

Read Now →

How to Get Random Item from Laravel Collection?

Read Now →

How to Convert Collection to Array in Laravel?

Read Now →

Laravel Collection keys() Method Example

Read Now →

Laravel Collection Has Method Example

Read Now →

Laravel Collection Duplicates Method Example

Read Now →

Laravel Collection SortByDesc Tutorial with Examples

Read Now →

Laravel Collection SortBy Tutorial with Examples

Read Now →

Laravel Collection Merge | How to Merge Two Eloquent Collection?

Read Now →

Laravel Collection Unique | Remove Duplicates from Collection Laravel

Read Now →