1. Go to this page and download the library: Download dhcmega/laravel-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/ */
dhcmega / laravel-meta example snippets
/**
* 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');
}
use Dhcmega\Metable\Metable;
class Post extends Eloquent
{
use Metable;
}
class Post extends Eloquent
{
protected $metaTable = 'posts_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']);