jaybizzle / hasmeta
Access model meta data as if it was a property on your model
Installs: 1 849
Dependents: 0
Suggesters: 0
Security: 0
Stars: 28
Watchers: 8
Forks: 4
Open Issues: 1
Requires
- php: >=5.4.0
- illuminate/support: 4.2.*|5.0.*
This package is auto-updated.
Last update: 2024-11-06 09:54:14 UTC
README
A Laravel trait to access model meta data as if it was a property on your model
Installation
Run composer require jaybizzle/hasmeta 0.1.*
or add "jaybizzle/hasmeta": "0.1.*"
to your composer.json
file
In the Model that you want to utilise HasMeta
add the following properties
use Jaybizzle\Hasmeta\HasMetaTrait; protected $meta_model = 'ModelName'; // the name of your meta data model protected $meta_foreign_key = 'user_id'; // the foreign key of your main model protected $meta_primary_key = 'meta_id'; // the primary key of you meta data model protected $meta_key_name = 'dataName'; // the column name that stores your meta data key name protected $meta_value_name = 'dataValue'; // the column name that stores your meta data value
Real World Example
Nothing like a simple example to explain things
###Setup
users
table
users_meta
table
User.php
model
class User extends Eloquent { use Jaybizzle\Hasmeta\HasMetaTrait; protected $meta_model = 'UserMeta'; protected $meta_foreign_key = 'user_id'; protected $meta_primary_key = 'id'; protected $meta_key_name = 'meta_name'; protected $meta_value_name = 'meta_value'; /** * The database table used by the model. * * @var string */ protected $table = 'users'; /** * The primary key on the table * * @var string */ protected $primaryKey = 'id'; // ... }
UserMeta.php
model
class UserMeta extends Eloquent { /** * The database table used by the model. * * @var string */ protected $table = 'users_meta'; /** * The primary key on the table * * @var string */ protected $primaryKey = 'id'; // ... }
###Usage
Now we can simply do this for getting meta data...
$user = User::find(1); echo $user->gender; // Will output 'Male'
We can save meta data easily too...
$user = User::find(1); $user->gender = 'Female'; $user->save();
Delete meta...
$user = User::find(1); $user->gender = null; $user->save();
New meta...
$user = User::find(1); $user->anything_you_want = 'some lovely value'; $user->save();