Package Data | |
---|---|
Maintainer Username: | chrismichaels84 |
Maintainer Contact: | phoenixlabsdev@gmail.com (Michael Wilson) |
Package Create Date: | 2014-08-15 |
Package Last Update: | 2023-02-06 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-22 03:13:02 |
Package Statistics | |
---|---|
Total Downloads: | 46,432 |
Monthly Downloads: | 9 |
Daily Downloads: | 1 |
Total Stars: | 56 |
Total Watchers: | 3 |
Total Forks: | 24 |
Total Open Issues: | 2 |
Attach meta data to Laravel's Eloquent models.
1.9.*
1.8.*
1.7.*
1.6.*
1.5.*
.1.3
1.2
To use for Laravel 4, see version 1.2.*
Install through Composer.
"require": {
"phoenix/eloquent-meta": "1.6.*"
}
Please note only php 5.6
and 7
+ are supported.
If you are using EloquentMeta and Eloquent without using Laravel, you will also have to setup Eloquent as detailed in its documentation.
If you are using Laravel, then you'll want to include the ServiceProvider that will register commands and the like. Update config/app.php
to include a reference to this package's service provider in the providers array.
'providers' => [
'Phoenix\EloquentMeta\ServiceProvider'
]
If you are using Laravel, run the migration php artisan vendor:publish
and php artisan migrate
to create the database table.
If you are not using Laravel then you must create the table manually.
CREATE TABLE meta
(
id INTEGER PRIMARY KEY NOT NULL,
metable_id INTEGER NOT NULL,
metable_type TEXT NOT NULL,
key TEXT NOT NULL,
value TEXT NOT NULL
);
CREATE UNIQUE INDEX meta_key_index ON meta (key);
CREATE UNIQUE INDEX meta_metable_id_index ON meta (metable_id);
Add the trait to all models that you want to attach meta data to:
use Illuminate\Database\Eloquent\Model;
use Phoenix\EloquentMeta\MetaTrait;
class SomeModel extends Model
{
use MetaTrait;
// model methods
}
Then use like this:
$model = SomeModel::find(1);
$model->getAllMeta();
$model->getMeta('some_key', 'optional default value'); // default value only returned if no meta found.
$model->updateMeta('some_key', 'New Value');
$model->deleteMeta('some_key');
$model->deleteAllMeta();
$model->addMeta('new_key', ['First Value']);
$model->appendMeta('new_key', 'Second Value');
You can also define a specific meta model for a meta type. For instance, your User model can use UserMeta model with custom methods and all. Using the example above:
use Illuminate\Database\Eloquent\Model;
use Phoenix\EloquentMeta\MetaTrait;
class SomeModel extends Model
{
use MetaTrait;
protected $meta_model = 'Fully\Namespaced\SomeModelMeta';
// model methods
}
Then in SomeModelMeta simply extends Phoenix\EloquentMeta\Meta. You may now add custom methods to the meta model. You may also dictate which table the metadata is saved to by adding
protected $table = "whatever_table";
If you are using EloquentMeta independent of Laravel, then you will have to create the database table manually.
If you are using Laravel, then include the service provider in your config/app.php
'providers' => [
'Phoenix\EloquentMeta\ServiceProvider'
]
Then run php artisan generate:metatable table_name
to create the migration and run php artisan migrate
to build the table.
Please se [CONTRIBUTING.md] for more information and for testing.
Contributors
Many thanks to Boris Glumpler and ScubaClick for the original package!