How to create a Laravel Collection From JSON
Today i have to create a laravel collection from a JSON object. I search on the internet and I cant find the best solution anywhere.. I saw too many articles related to converting laravel collection into JSON .. after reading so many documents i found a way to Create a laravel collection from Json.
So here in this tutorial, I will share you
How to create a Laravel Collection from JSON [example 1]
//get json data $getjson= Http::acceptJson()->get('enterJSON_API-url_here')->throw()->json(); // Creating Laravel collection $laravelcollection = collect($getjson)->values();
So you can use this laravel collection like any other model.
How to create a Laravel Collection from JSON [example 2]
// get json using curl $curl = curl_init(); curl_setopt_array($curl, [ CURLOPT_URL => "Enter API URL HERE", CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "GET", ]); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); // decode json $response = json_decode($response, true); //create laravel collection $collection = collect($response);
Hope this Helps ..
Checkout more awesome laravel tutorialsÂ
Post Your Questions on our forum
Post a question on ForumShare with your friends:
How to integrate Paypal API in Laravel
Are you looking to integrate Paypal API in your Laravel project for seamless payment processing? Look no further! In this […]
April 3, 2024
How to integrate Razorpay API in Laravel
Integrating payment gateways into web applications has become an essential part of e-commerce websites. In this tutorial, we will discuss […]
April 3, 2024
Laravel 11 Ajax CRUD Operation Tutorial Example
**Mastering CRUD Operations with Laravel 11 Ajax: A Comprehensive Tutorial** In the world of web development, interaction between the front-end […]
April 3, 2024
Login as Client in Laravel – Login with user id
**Unlock the Power of Laravel with Login as Client – Login with User ID** Laravel, the popular PHP framework, offers […]
April 3, 2024
Digital Marketing Toolkit
Get Free Access to Digital Marketing Toolkit. You can use all our tools without any limits
Get Free Access Now