Meta data for Laravel's Eloquent model

Installs: 85

Dependents: 0

Stars: 9

Watchers: 1

Forks: 1

Open Issues: 3

Language: PHP

1.0.3 2014-11-21 16:47 UTC

README

The meta trait gives a model the ability to dynamically set and get data on a model.

Installation

Require through composer

"require": {
    "enginebit/meta": "1.0.*"
}

Make sure the meta tables are generated with the following scheme

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUserDataTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('user_data', function(Blueprint $table)
        {
            $table->increments('id');

            $table->integer('user_id')->unsigned()->index();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

            $table->string('type')->default('null');

            $table->string('key')->index();
            $table->text('value'); // or $table->string('value')->index();

            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('user_data');
    }

}

Use the trait on a model

<?php

use Enginebit\Meta\MetaTrait;

class User extends Eloquent {

    use MetaTrait;

    /**
     *  The Eloquent models table name
     */
    protected $table = 'users';

    /**
     *  The table name for the meta data
     */
    protected $metaTable = 'user_data';

    /**
     *  Default key name is 'model_id'
     */
    protected $metaKeyName = 'user_id';

    /**
     *  To set a custom data model
     */
    protected $metaModel = 'Acme\User\Data';

}

Setters and Getters

<?php

$user = User::find(1);

$user->setMeta('first_name','John');
$user->setMeta('last_name','Doe');

$user->save();

echo $user->getMeta('first_name');
// or
echo $user->first_name; // This wil first check the model and if null is returned it wil check the meta model

Saving datetime objects

$user = User::find(1);

$user->setMeta('activated_at',Carbon::now());

$user->save();

// On another request

$activated_at = $user->getMeta('activated_at'); // Will return the carbon object
// or
$activated_at = $user->activated_at;

Saving Eloquent models

$post = Post::find(1);

$user->setMeta('post',$post); // Will be saved as a string like 'Acme\Posts\Post#1'

$user->save();

// On another request

$user = User::find(1);

$post = $user->getMeta('post'); // Will return the model
// or
$post = $user->post;

Unset data

The meta data will only really be deleted is the save method is called on the model.

<?php

$user = User::find(1);

$user->deleteMeta('post');
// or
unset($user->post);

$user->save