Download the PHP package elhareth/laravel-eloquent-metable without Composer
On this page you can find all versions of the php package elhareth/laravel-eloquent-metable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download elhareth/laravel-eloquent-metable
More information about elhareth/laravel-eloquent-metable
Files in elhareth/laravel-eloquent-metable
Package laravel-eloquent-metable
Short Description A Laravel package to extend Eloquent models data
License MIT
Homepage https://github.com/elhareth/laravel-eloquent-metable
Informations about the package laravel-eloquent-metable
Laravel-Eloquent-Metable
Laravel-Eloquent-Metable is a package for extending laravel Eloquent models without messing up with the database schema.
Example Usage
Adding metadata to an eloquent model
Query scope to fetch model by it's metadata
Get attached metadata
Installation
Add the package to your Laravel app using composer
Run the migrations to add the required table to your database.
Add the Elhareth\LaravelEloquentMetable\IsMetable
trait to any eloquent model class that you want to be able to attach metadata to.
Documentation
Handling Meta
The IsMetable
trait will add metalist()
method to your model, which meant to handle the realtion between your model and the meta data model Metable
.
Attaching MetaData
Attach meta to a model with the setMeta()
method. The method accepts three arguments: a string to use as a meta name, a value and additionally a string for group. The value argument will accept a number of different inputs, which will be converted to a string for storage in the database.
If you want to add many meta records you may pass an array as first parameter
Also you pass a second param as meta data group
Be Carefull !
if you adding a meta record with value of type array if it has value/group key it will be treated as $value
/ $group
params, so when you adding an array that may have these keys consider using setMeta()
method where the value param is treated as value no matter what it does contain.
Retrieving MetaData
You can retrieve the value of the meta at a given key with the getMeta()
method.
The method getMeta
accepts three arguments; getMeta(name: $name, default: $default nullable $nullable)
The first the name of meta record
The second parameter to specify a default value to return if no meta has been set at that key.
The third parameter is a boolean to set whether to treat null values as not found
You may retrieve a collection of meta depending on specifec group
Checking For Presence of MetaData
You can check if a value has been assigned to a given key with the hasMeta()
method.
This method will return true if a record is found on database.
You may pass another boolean value to control how to treat null values.
Queued Metables
You may mass assign metables and they will be attached once the model is created
Pre Metables
You may define default metables for each model and they'll be added once the model is created.
To do so you should implement Elhareth\LaravelEloquentMetable\IsMetableInterface
> And define a method defaultMetables()
which returns an array of metables you want to automatically attach to your model.
You can get list of model default metables by getDefaultMetables()
.
Deleting MetaData
To remove the meta stored at a given key, use removeMeta()
.
To delete all meta records from a model, use deleteMetaRecords()
.
Attached meta is automatically purged from the database when a IsMetable
model is manually deleted. Metaable will not
be cascaded if the model is deleted by the query builder.
Eager Loading MetaData
When working with collections of IsMetable
models, be sure to eager load the meta relation for all instances together to avoid repeated database queries (i.e. N+1 problem).
Eager load from the query builder:
Lazy eager load from an Eloquent collection:
You can also instruct your model class to always
eager load the meta relationship by adding 'metalist'
to your model's $with
property.
Querying Meta
The IsMetable
trait provides a number of query scopes to facilitate modifying queries based on the meta attached to your models
Checking for Presence of a key
To only return records that have a value assigned to a particular key, you can use whereHasMeta()
. You can also pass an array to this method, which will cause the query to return any models attached to one or more of the provided keys.
You can also query for records that does not contain a meta key using the whereDoesntHaveMeta()
. Its signature is identical to that of whereHasMeta()
.
You can restrict your query based on the value stored at a meta key. The whereMeta()
method can be used to compare the value using any of the operators accepted by the Laravel query builder's where()
method.
The whereMetaIn()
method is also available to find records where the value is matches one of a predefined set of options.
License
This package is released under the MIT license (MIT).
All versions of laravel-eloquent-metable with dependencies
ext-json Version *
illuminate/collections Version ^10.0
illuminate/contracts Version ^10.0
illuminate/database Version ^10.0
illuminate/support Version ^10.0