Download the PHP package iteks/laravel-json without Composer
On this page you can find all versions of the php package iteks/laravel-json. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-json
The Laravel JSON package is a powerful and versatile tool designed to enhance the handling of JSON data within Laravel applications. With its intuitive API, developers can effortlessly convert JSON files into Laravel collections or associative arrays, facilitating easy data manipulation and access. Whether you're dealing with configuration files, dataset imports, or any JSON-formatted data source, this package simplifies the process, allowing you to focus on building feature-rich applications. Built with flexibility in mind, it supports optional attribute filtering, enabling precise data retrieval tailored to your needs. Additionally, enforce any JSON model column's data structure with column definitions, ensuring JSON data consistency with every interaction. Perfect for projects of all sizes, Laravel JSON aims to streamline your development workflow, making JSON data handling a breeze.
Offered by iteks, Developed by jeramyhing.
Get Started
Requires PHP 8.1+
Install Laravel JSON via the Composer package manager:
Usage
Include the Json facade.
- Sample JSON Dataset
- JSON Helpers
- Json::toCollection()
- Json::toArray()
- JSON Trait (DefinesJsonColumns)
- Json::enforceDefinition()
Sample Json Dataset
This JSON dataset is used in the Json::toArray() method examples.
top
JSON Helpers
Json::toCollection()
The toCollection
method converts a JSON file into a Laravel collection of collections. This is particularly useful when you need to manipulate JSON data with the convenience and power of Laravel's Collection methods.
Without Argument Usage
When you use toCollection without specifying an attribute, it will simply convert the entire JSON file into a collection where each element is itself a collection representing the JSON objects.
With Argument Usage
When you provide an attribute name as the second argument, toCollection will create a collection where each item is the value of the specified attribute from the JSON objects.
top
Json::toArray()
The toArray
method converts a JSON file into a PHP array. This method is ideal for when you need a simple array representation of your JSON data for further processing or when Laravel's Collection methods are not necessary.
Without Argument Usage
Without specifying an attribute, toArray converts the entire JSON file into a nested array, with each element being an associative array representing the JSON objects.
With Argument Usage
Providing an attribute name as the second argument, toArray will generate an array containing only the values of the specified attribute from each JSON object.
top
JSON Trait (DefinesJsonColumns)
The DefinesJsonColumns
trait and complementary enforceDefinition
method allow you to define and enforce a JSON data structure for your JSON database columns. This ensures that any interaction with a JSON data column will consistently contain data that is structured according to its JSON definitions.
Simply apply the DefinesJsonColumns
trait to your model that has JSON column(s) to define. Define the JSON structure with the $jsonDefinitions
property on the model and begin using the enforceDefinition
to enforce the column definition on your JSON input.
Import the model trait and configure your JSON definitions.
You can configure definitions for multiple JSON columns.
Json::enforceDefinition()
You can use the enforceDefinition
method anywhere in your application logic with target JSON input that you intend to insert into your model's JSON column.
If the input JSON is
null
or missing any defined keys, the definition will still be enforced by adding the missing keys withnull
values. If the input JSON contains additional key value pairs that are not in the JSON column definition, they will be excluded.
Usage in a form request's prepareForValidation
method
Apply the enforceDefinition
method on the target request attribute that contains the JSON input. Pass the model class, column, and request attribute's JSON value.
top