PHP code example of zoha / laravel-meta

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

    

zoha / laravel-meta example snippets


'providers' => [
    ...
    Zoha\Meta\MetaServiceProvider::class,
    ...
]
'aliases' => [
    ...
    'Meta' => Zoha\Meta\Facades\MetaFacade::class,
    ...
]


use Zoha\MetableModel;

class Post extends MetableModel
{
    ...
}

use Zoha\Metable;

class Post extends Model
{
    use Metable;
    ...
}

$post->createMeta('key' , 'value');

$post->createMeta([
    'key1' => 'value1',
    'key2' => 'value2'
]);

$post->updateMeta('key' , 'new value');

$post->updateMeta([
    'key1' => 'new value 1',
    'key2' => 'new value 2'
]);

$post->setMeta('key' , 'value'); // create meta
$post->setMeta('key' , 'new value'); // update meta

$post->setMeta([
    'key1' => 'value 1',
    'key2' => 'value 2'
]);

$post->meta->key1 = 'value';
$post->meta->key2 = 'value2';
$post->meta->save();

//return meta value or null if meta not exists
$post->getMeta('key');

// return value or 'default value ' if not exists or value is null
$post->getMeta('key' , 'default value') ;

$post->meta->key; // return meta value

// return a collection of all metas for this model
// no meta yet ? -> empty collection
$post->getMetas() ;

$post->deleteMeta(); // delete all meta of this model

$post->deleteMeta('key'); // delete a specific meta

$post->truncateMeta(); // delete all model meta

$post->hasMeta(); // return true if this model has at least one meta
$post->hasMeta('key'); // return true or false

$post->setMeta('key' , null); // set key to null
$post->hasMeta('key'); // return false
$post->hasMeta('key' , true); // return true

$post->setMeta('key' , null); // set key to null
$post->existsMeta('key'); // return true

$post->setMeta('key' , 3);
$post->increaseMeta('key'); // meta value will change to 4

$post->setMeta('key' , 3.5);
$post->increaseMeta('key'); // meta value will change to 4.5

$post->setMeta('key' , 3.5);
$post->increaseMeta('key' , .1); // meta value will change to 3.6

$post->setMeta('key2' , 'not integer value');
$post->increaseMeta('key2'); // meta value will not change

$post->setMeta('key' , 3);
$post->increaseMeta('key',3); // meta value will change to 6

$post->setMeta('key' , 3);
$post->decreaseMeta('key'); // meta value will change to 2

$post->setMeta('key' , 3.5);
$post->decreaseMeta('key'); // meta value will change to 2.5

$post->setMeta('key' , 3.5);
$post->decreaseMeta('key', .1); // meta value will change to 3.4

$post->setMeta('key2' , 'not integer value');
$post->decreaseMeta('key2'); // meta value will not change

$post->setMeta('key' , 3);
$post->decreaseMeta('key',3); // meta value will change to 0

$result = Post::whereMeta('key','value');
// you can use operator :
$result = Post::whereMeta('key', '>' , 100);

// you can use multiple whereMeta Clause
$result = Post::whereMeta('key', '>' , 100)
                            ->whereMeta('key' , '<' , 200);
//orWhereMeta clause
$result = Post::whereMeta('key', '>' , 100)
                            ->orWhereMeta('key' , '<' , 50);
//branched clauses
$result = Post::where(function($query){
    $query->where(function($query){
        $query->whereMeta('key1' , 'value1');
        $query->orWhereMeta('key1' , 'value2');
    });
    $query->whereMeta('key2' , 'like' , '%value%');
    $query->WhereMeta('key3' , '>' , 100);
});

// all of below conditions will converted to 'AND'
$result = Post::whereMeta([
    'key1' => 'value1',
    [ 'key2' , '!=' , 'value2' ],
    [ 'key3' , 'value3' ]
]);

$result = Post::orWhereMeta([
    'key1' => 'value1',
    [ 'key2' , '!=' , 'value2' ],
    [ 'key3' , 'value3' ]
]);

$result = Post::whereMetaIn('key', ['value1' , 'value2']);
$result = Post::whereMetaNotIn('key', ['value1' , 'value2']);

// multiple clauses
$result = Post::whereMetaIn('key', [1,2])
                            ->whereMetaIn('key2' , [1,2]);

// 'orWhere' clauses
$result = Post::whereMetaNotIn('key', [1,2,3])
                            ->orWhereMetaIn('key2', [1,2])
                            ->orWhereMetaNotIn('key3', [1,2]);

$result = Post::whereMetaBetween('key', [0,100]);
$result = Post::whereMetaNotBetween('key', [0,100]);

// multiple clauses
$result = Post::whereMetaBetween('key', [100,200])
                            ->whereMetaBetween('key2' , [100,200]);

// 'orWhere' clauses
$result = Post::whereMetaNotBetween('key', [1000,8000])
                            ->orWhereMetaBetween('key2', [1,5])
                            ->orWhereMetaNotBetween('key3', [0,100]);

$result = Post::whereMetaNull('key');
$result = Post::whereMetaNotNull('key');

// multiple clauses
$result = Post::whereMetaNull('key')
                            ->whereMetaNull('key2');

// 'orWhere' clauses
$result = Post::whereMetaNotNull('key')
                            ->orWhereMetaNull('key2')
                            ->orWhereMetaNotNull('key3');

//filter records that has at least one meta
$result = Post::whereMetaHas();

$result = Post::whereMetaHas('key');
$result = Post::whereMetaHas('key' , true); //count null values

$result = Post::whereMetaDoesntHave('key');
$result = Post::whereMetaDoesntHave('key' , true); //count null values

// multiple clauses
$result = Post::whereMetaHas('key')
                            ->whereMetaDoesntHave('key2');

// 'orWhere' clauses
$result = Post::whereMetaDoesntHave('key')
                            ->orWhereMetaHas('key2')
                            ->orWhereMetaDoesntHave('key3');

Post::orderByMeta('price')->get();

Post::orderByMeta('price' , 'desc')->get();

Post::orderByMeta('price')->orderByMeta('likes' , 'desc')->get();


$posts = Post::withMeta()->get(); // will return all posts results with their meta values

$post->setMeta('key' , '123' , 'integer');
$post->meta->key; // 123 ( integer )

$post->setMeta('key' , '123.45' , 'integer');
$post->meta->key; // 123 ( integer - decimal dropped)

$post->setMeta('key' , '123.45' , 'float');
$post->meta->key; // 123.45 ( float )
//----------------------------------
$post->setMeta('key' , [1,2,3] , 'json');
$post->meta->key; // "[1,2,3]" ( string - json )
//----------------------------------
$post->setMeta([
    'key1' => 'value',
    'key2' => 2
    'key3' => [1,2,3]
],'string' ); // all values will converted to string when you try to get them
$post->meta->key2; // "2" ( string )
$post->meta->key3; // "[1,2,3]" ( string json )

$post->setMeta('key' , 123);
$post->getMeta('key' , 'null' , 'string'); // "123" (string)
//----------------------------------
$post->setMeta('key' , [1,2,3] , 'string');
$post->getMeta('key' , 'null'); // "[1,2,3]" (string)
$post->getMeta('key' , 'null' , 'array'); // [1,2,3] (array)
$post->getMeta('key' , 'null' , 'boolean'); // true (boolean)

'tables' => [
    'default' => 'meta',
    'custom'  => [
        'users_meta'
    ],
]

use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
    use \Zoha\Metable;

    protected $metaTable = 'users_meta';
}


use Zoha\Meta\Models\Meta;
$count = Meta::count();

use Zoha\Meta\Models\Meta;

$post->meta->key1; // exmaple : return 'test'
Meta::truncate();
$post->meta->key1; // still return 'test'

$post->refreshLoadeMeta();
$post->meta->key1; // will return null

Schema::create('meta', function (Blueprint $table) {
    $table->increments('id');
    $table->string('key',110);
    $table->text('value')->nullable();
    $table->string('type')->default(Meta::META_TYPE_STRING);
    $table->boolean('status')->default(true);
    $table->string('owner_type',80);
    $table->integer('owner_id');
    $table->unique(['key','owner_type','owner_id']);
    $table->timestamps();
});

 $ php artisan migrate

$ php artisan migrate