Coder Social home page Coder Social logo

laravel-meta's Introduction

Fluent Meta Data for Eloquent Models

Laravel Source Build Status License

Metable Trait adds the ability to access meta data as if it is a property on your model. Metable is Fluent, just like using an eloquent model attribute you can set or unset metas. Follow along the documentation to find out more.

Installation

Composer

Add this to your composer.json file, in the require object:

"kodeine/laravel-meta": "dev-master"

After that, run composer install to install the package.

Migration Table Schema

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

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

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

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

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

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

Configuration

Model Setup

Next, add the Metable trait to each of your metable model definition:

use Kodeine\Metable\Metable;

class Post extends Eloquent
{
    use Metable;
}

Metable Trait will automatically set the meta table based on your model name. Default meta table name would be, model_meta. In case you need to define your own meta table name, you can specify in model:

class Post extends Eloquent
{
    protected $metaTable = 'posts_meta'; //optional.
}

Gotcha

When you extend a model and still want to use the same meta table you must override getMetaKeyName function.

class Post extends Eloquent
{
    
}

class Slideshow extends Post
{
    protected function getMetaKeyName()
    {
        return 'post_id' // The parent foreign key 
    }   
}

Working With Meta

Setting Content Meta

To set a meta value on an existing piece of content or create a new data:

Fluent way, You can set meta flawlessly as you do on your regular eloquent models. Metable checks if attribute belongs to model, if not it will access meta model to append or set a new meta.

$post = Post::find(1);
$post->name = 'hello world'; // model attribute
$post->content = 'some content goes here'; // meta data attribute
$post->save(); // save attributes to respective tables

Or

$post = Post::find(1);
$post->name = 'hello world'; // model attribute
$post->setMeta('content', 'Some content here');
$post->save();

Or set multiple metas at once:

...
$post->setMeta([
    'content' => 'Some content here',
    'views' => 1,
]);
$post->save();

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:

Fluent way to unset.

$post = Post::find(1);
$post->name // model attribute
unset($post->content) // delete meta on save
$post->save();

Or

$post->unsetMeta('content');
$post->save();

Or unset multiple metas at once:

$post->unsetMeta('content,views');
// or
$post->unsetMeta('content|views');
// or
$post->unsetMeta('content', 'views');
// or array
$post->unsetMeta(['content', 'views']);

$post->save();

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:

Fluent way, Metable is clever enough to understand $post->content is an attribute of meta.

if (isset($post->content)) {

}

Retrieving Meta

To retrieve a meta value on a piece of content, use the getMeta method:

Fluent way, You can access meta data as if it is a property on your model. Just like you do on your regular eloquent models.

$post = Post::find(1);
dump($post->name);
dump($post->content); // will access meta.

Or

$post = $post->getMeta('content');

Or specify a default value, if not set:

$post = $post->getMeta('content', 'Something');

You may also retrieve more than one meta at a time and get an illuminate collection:

// using comma or pipe
$post = $post->getMeta('content|views');
// or an array
$post = $post->getMeta(['content', 'views']);

Retrieving All Metas

To fetch all metas associated with a piece of content, use the getMeta without any params

$metas = $post->getMeta();

Retrieving an Array of All Metas

To fetch all metas associated with a piece of content and return them as an array, use the toArray method:

$metas = $post->getMeta()->toArray();

Meta Table Join

When you need to filter your model based on the meta data , you can use meta scope in Eloquent Query Builder.

$post = Post::meta()
    ->where(function($query){
          $query->where('posts_meta.key', '=', 'revision')
                ->where('posts_meta.value', '=', 'draft');
    })

laravel-meta's People

Contributors

hammat avatar kodeine avatar olsgreen avatar ray1618 avatar shemi avatar sumityadav avatar todiadiyatmo avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.