PHP code example of kodeine / laravel-meta

1. Go to this page and download the library: Download kodeine/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/ */

    

kodeine / laravel-meta example snippets


   class User extends Model{
       use Metable{
           __get as __metaGet
       }
   

protected $metaTable = 'custom_meta_table';

protected $metaKeyName = 'custom_foreign_key';

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

        $table->bigInteger('post_id')->unsigned();
        $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 Kodeine\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 = 'hello world'; // model attribute
$post->setMeta('content', 'Some content here');
$post->save();

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

...
$post->setAttributes([
    'name' => 'hello world'; // model attribute
    'content' => 'Some content here',
    'views' => 1,
]);
$post->save();

$post->content = 'some content goes here'; // meta data attribute
$post->saveMeta(); // will save metas to database but won't save the model itself

$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)) {

}
// or
if ($post->hasMeta('content')){

}

$post->hasMeta(['content','views']); // returns true only if all the metas exist
// or
$post->hasMeta('content|views');
// or
$post->hasMeta('content,views');

$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']);
// specify default values
$post->getMeta(['content', 'views'],['content'=>'something','views'=>0]);
// or specify one default value for all missing metas
$post->getMeta(['content', 'views'],'none');// result if the metas are missing: ['content'=>'none','views'=>'none']
// without specifying default value result will be null
$post->getMeta(['content', 'views']);// result if the metas are missing: ['content'=>null,'views'=>null]

protected $disableFluentMeta = true;

$post->content='something';// will not set meta. original laravel action will be taken
$post->content;// will not retrieve meta
unset($post->content);// will not unset meta
isset($post->content);// will not check if meta exists

$metas = $post->getMeta();

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


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


$post = Post::with(['metas'])->get();

/* Post model */
public $hideMeta = true; // Do not add metas to array

use Kodeine\Metable\Metable;
use Kodeine\Metable\HasMetaEvents;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use Metable,HasMetaEvents;
}

use App\Events\UserMetaSaved;
use Kodeine\Metable\Metable;
use Kodeine\Metable\HasMetaEvents;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use Metable,HasMetaEvents;

    protected $dispatchesEvents = [
        'metaSaved' => UserMetaSaved::class,
    ];
}

use Kodeine\Metable\Metable;
use Kodeine\Metable\HasMetaEvents;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use Metable,HasMetaEvents;

    protected static function booted()
    {
        static::metaCreated(function ($user, $meta) {
            //
        });
    }
}

class UserObserver
{
    public function metaCreated(User $user,$meta)
    {
        //
    }
}

   public $defaultMetaValues = [
      'is_user_home_sick' => false,
   ];