fromJson() in laravel 12.8

00:00
BACK TO HOME

fromJson() in laravel 12.8

10xTeam April 16, 2025 1 min read

In Laravel 12.8, the fromJson() method was introduced as part of the Collection class. This method simplifies the process of creating a collection from a JSON string. Here’s how it works:

Usage

  • Before Laravel 12.8: You would create a collection from JSON using json_decode() followed by manually wrapping it in a Collection instance:
$collection = new Collection(json_decode($json, true));
  • After Laravel 12.8: You can directly use the fromJson() method:
$collection = Collection::fromJson($json);

Features

  • The fromJson() method supports additional arguments like depth and flags, which are typically passed to PHP’s native json_decode() function:
$collection = Collection::fromJson(json: $json, flags: JSON_THROW_ON_ERROR);

This enhancement streamlines JSON parsing for collections, making code cleaner and more intuitive12.

  1. https://laravel-news.com/index.php/laravel-12-8-0 

  2. https://fossies.org/diffs/laravel-framework/12.8.0_vs_12.8.1/CHANGELOG.md-diff.html 


Join the 10xdev Community

Subscribe and get 8+ free PDFs that contain detailed roadmaps with recommended learning periods for each programming language or field, along with links to free resources such as books, YouTube tutorials, and courses with certificates.

Audio Interrupted

We lost the audio stream. Retry with shorter sentences?