Download the PHP package mmanos/laravel-metable without Composer
On this page you can find all versions of the php package mmanos/laravel-metable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-metable
Meta Package for Laravel 4
This package adds meta support to your Laravel application. You can configure it to attach meta to any of your existing Eloquent models.
Installation
Composer
Add this to your composer.json file, in the require object:
After that, run composer install to install the package.
Service Provider
Register the Mmanos\Metable\MetableServiceProvider
in your app
configuration file.
Configuration
Metas Migration and Model
First you'll need to publish a metas
table and a Meta
model. This table will hold a summary of all meta created by your metable models.
Note: Modify the last parameter of this call to change the table/model name.
Note: You may publish as many meta tables as you need, if you want to keep the meta separate for different types of content, for example.
Metable Migration
Next, publish a migration for each type of content you want to attach meta to. You may attach meta to as many types of content as you wish. For example, if you want to be able to add meta to both a users
table and a blog_posts
table, run this migration once for each table.
Run Migrations
Once the migration has been created, simply run the migrate
command.
Model Setup
Next, add the Metable
trait to each metable model definition:
Then you need to specify the meta model as well as the metable table to use with your model:
Syncing Custom Attributes
Sometimes you will want to have some of the same fields in your content table synced to the metable table records. This will allow you to filter and sort by these attributes when querying the metable table. Luckily this system will automatically sync any fields you define to the metable table records any time there are changes.
To get started, modify the metable migration file to include your additional fields.
Then, tell your model which fields it needs to sync:
Now every time you create or update a model, these fields will by synced to all metable table records for the piece of content.
Syncing Deleted Content
This package will automatically delete all metable table records for a piece of content when that piece of content is deleted.
If you are using the SoftDeletingTrait
and you are syncing the deleted_at
column to your metable table records, this package will automatically soft-delete all metable table records for a piece of content when that piece of content is deleted. If the content is restored, then the metable table records are restored as well.
Working With Meta
Setting Content Meta
To set a meta value on an existing piece of content:
Or set multiple metas at once:
Note: If a piece of content already has a meta the existing value will be updated.
Unsetting Content Meta
Similarly, you may unset meta from an existing piece of content:
Or unset multiple metas at once:
Note: The system will not throw an error if the content does not have the requested meta.
Checking for Metas
To see if a piece of content has a meta:
Retrieving Meta
To retrieve a meta value on a piece of content, use the meta
method:
Or specify a default value, if not set:
You may also retrieve more than one meta at a time and get back an array of values:
Retrieving All Metas
To fetch all metas associated with a piece of content, use the metas
relationship:
Retrieving an Array of All Metas
To fetch all metas associated with a piece of content and return them as an array, use the metasArray
method:
Querying for Content from Meta
Performing Queries
Now let's say you want to query for all content that has a given meta:
Or optionally specify the meta value to match:
Or optionally specify the meta value and operator to match:
These queries extend the same QueryBuilder
class that you are used to working with, so all of those methods work as well:
Note: The
update
anddelete
methods on a QueryBuilder object do not work for these queries.
You may query for content that has any of the given meta:
Or query for content that has any of the given meta that matches the given values:
Note: Query performance can be reduced for the
withAnyMeta
andwhereAnyMeta
queries if your queries match thousands of records or more.
And you may combine multiple filters:
Meta Contexts
Sometimes you might want to associate your metas (summary) table records with some custom context for your application. For example, say you have a companies
table and a users
table and each user belongs to a company. And now you also want to associate each meta record with a company allowing you to fetch all meta used by each individual company. In order to do so, we have to tell this package to be aware of this company context and modify it's queries accordingly.
To get started, make sure you modify your metas migration to include any context fields (company_id
, in this case). You might also need to update the unique index, if necessary.
Then modify your metable model by adding a metaContext
method:
Next modify your Meta
model (or whatever name you specified during configuration) to apply any contexts:
The applyQueryContext
method will adjust any meta queries used by this package to filter on company_id
.
The applyModelContext
method is called when creating a new Meta
record and should set any required context fields.
Finally, when performing queries, specify the context to apply: