1. Go to this page and download the library: Download necenzurat/eloquent-meta library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
necenzurat / eloquent-meta example snippets
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('MODEL_meta', function (Blueprint $table) {
$table->increments('id');
$table->integer('model_id')->unsigned()->index();
$table->foreign('model_id')->references('id')->on('MODEL')->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('MODEL_meta');
}
use necenzurat\EloquentMeta\Metable;
class Post extends Eloquent
{
use Metable;
}
class Post extends Eloquent
{
protected $metaTable = 'model_meta'; //optional.
}
$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
$post = Post::find(1);
$post->name // model attribute
unset($post->content) // delete meta on save
$post->save();
$post->unsetMeta('content');
$post->save();
$post->unsetMeta('content,views');
// or
$post->unsetMeta('content|views');
// or
$post->unsetMeta('content', 'views');
// or array
$post->unsetMeta(['content', 'views']);
$post->save();
if (isset($post->content)) {
}
$post = Post::find(1);
dump($post->name);
dump($post->content); // will access meta.
$post = $post->getMeta('content');
$post = $post->getMeta('content', 'Something');
// using comma or pipe
$post = $post->getMeta('content|views');
// or an array
$post = $post->getMeta(['content', 'views']);