PHP code example of elhareth / laravel-eloquent-metable

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

    

elhareth / laravel-eloquent-metable example snippets


$user = User::create([
    'name' => 'User Name',
    'email' => '[email protected]',
    'password' => 'password',
]);
$user->setMeta('gender', 'male');

$user = User::whereMeta('gender', 'male');

$gender = $user->getMeta('gender');

use Illuminate\Database\Eloquent\Model;
use Elhareth\LaravelEloquentMetable\IsMetable;

class User extends Model
{
    use IsMetable;
}

 $user->setMeta('key', 'value');
 $user->setMeta('key', 'value', null);
 $user->setMeta('key', 'value', 'group');

 $user->setMeta([
    'full_name' => 'Full Name',
    'gender' => 'male',
    'bio' => 'Lorem absum...',
 ]);

 $user->setMeta([
    'full_name' => 'Full Name',
    'gender' => 'male',
    'bio' => 'Lorem absum...',
 ], 'profile');

 $user->setMeta('key', [
    'value' => 'random-content',
 ]); // 'a:1:{s:5:"value";s:14:"random-content";};'
 $value = $user->getMeta('key'); // ['value' => 'random-content']

 $gender = $user->getMeta('gender');

 $user->getMeta('gender', 'male'); // will return 'male' if not set
 $user->getMeta('gender', 'male', false); // will return male if not set or if value is null
 $user->getMeta('gender', 'male', true); // will return male if not set or null if is set to null

 $profile = $user->getMetaGroup('profile'); // return collection

 if ($user->hasMeta('gender')) {
    // ...
 }

 if ($user->has(name: 'gender', nullable: true)) {
    // ...
 }

 $user = User::create([
    'name' => 'UserName',
    'email' => '[email protected]',
    'password' => 'password',
    'metables' => [
        'gender' => 'male', // OR
        'gender' => [
            'value' => 'male', // OR
        ],
        'gender' => [
            'value' => 'male',
            'group' => 'profile', // If not specified group is null
        ],
    ],
 ]);

 $user->getMeta('gender') // male

 use Elhareth\LaravelEloquentMetable\IsMetableInterface;
 use Illuminate\Database\Eloquent\Model;
 
 class User extends Model implements IsMetableInterface
 {
    /**
     * Default Metables
     * 
     * @return array
     */
    protected function defaultMetables(): array
    {
        return [
            'gender' => [
                'value' => null,
                'group' => 'profile',
            ],
            'bio' => [
                'value' => null,
                'group' => 'profile',
            ],
            'locale' => [
                'value' => 'en',
                'group' => null,
            ],
            'avatar' => [
                'value' => 'avatar.png',
            ],
            'theme' => 'dark',
        ];
    }
 }

 $user->removeMeta('bio');

 $user->deleteMetaRecords();

 $user->delete(); // will delete attached meta
 User::where(...)->delete() // will NOT delete attached meta

 $users = User::with('metalist')->where(...)->get();

 $users->load('metalist');

 class User extends Model {
     use IsMetable;
 
     protected $with = ['metalist'];
 }

 $users = User::whereHasMeta('gender')->get();
 $users = User::whereHasMeta(['gender', 'bio'])->get();

 $users = User::whereDoesntHaveMeta('gender')->get();
 $users = User::whereDoesntHaveMeta(['gender', 'bio'])->get();

 $users = User::whereMeta('letters', ['a', 'b', 'c'])->get();
 

 $users = User::whereMetaIn('country', ['CAN', 'USA', 'MEX']);
bash
php artisan migrate