How to Convert JSON to Collection in 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
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Collection Sort By Date Example
- Laravel Collection Remove Item by Value Example
- How to Remove Null and Empty Values from Laravel Collection?
- Laravel Collection Remove Last Item Example
- Laravel Collection Remove First Item Example
- How to Get Random Item from Laravel Collection?
- How to Convert Collection to Array in Laravel?
- Laravel Collection keys() Method Example
- Laravel Collection Has Method Example
- Laravel Collection Duplicates Method Example
- Laravel Collection SortByDesc Tutorial with Examples
- Laravel Collection SortBy Tutorial with Examples
- Laravel Collection Merge | How to Merge Two Eloquent Collection?
- Laravel Collection Unique | Remove Duplicates from Collection Laravel