Download the PHP package moirei/laravel-model-data without Composer
On this page you can find all versions of the php package moirei/laravel-model-data. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download moirei/laravel-model-data
More information about moirei/laravel-model-data
Files in moirei/laravel-model-data
Package laravel-model-data
Short Description Easily handle data fields in your Eloquent models
License MIT
Homepage https://github.com/augustusnaz/laravel-model-data
Informations about the package laravel-model-data
laravel-model-data
This package allows you to access attribute fields in your model or use a data field without any changes to your migrations.
Features
- Use extra data on eloquent model without defining a new column
- Easily interact with multiple custom fields on your model as data fields
collect
any data and save into any model with theHasData
trait
Installation & Setup
You can install the package via composer;
Add the MOIREI\ModelData\HasData
trait to your model;
Using the package's model
Optional if using a predefined attribute/column in your models.
Publish the migration with;
Run the migrate command to create the necessary table;
Mode 1: Persist data outside your model
This is the default mode if the $model_data
property is falsy.
Mode 2: Persist data in your model
Define the data column (s) in your migration;
Then define a model_data
variable in your model. This is the name of the storage column(s).
Usage
Accessing the data
The below uses data
if in mode 1 or the value of $model_data
is "data"
;
Basic access:
Access as arrays:
Calling as function.
All existing data can be overridden by assigning an array;
With get
and set
;
get
with default:
Multiple/custom data fields (mode 2)
The above example uses data
field which is the default for external mode. To allow multiple access with custom names,
Access with
Persisting data
Modify and save into a different model with
Collections
This packages supplies Collections functions pinch
and save
globally.
This means you can collect and save any data into any model that has the HasData
trait;
The pinch
function simply allows you to access a collection's underlying array using the dot notation.
The key
option defaults to data
.
Credits
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-model-data with dependencies
illuminate/database Version ^5.6|^6.0|^7.0|^8.0
illuminate/support Version ^5.6|^6.0|^7.0|^8.0