PHP code example of esslassi / metable

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

    

esslassi / metable example snippets


'providers' => [
    ...
    Esslassi\Metable\MetableServiceProvider::class,
    ...
];

php artisan vendor:publish --provider="Esslassi\Metable\MetableServiceProvider::class"

return [

    /*
	 *
     * You can customize default table name by relacing 'meta' to your own
     *
     */

    'tables' => [

        // default table for all models

        'default' => 'meta'
        
    ]
];

php artisan migrate

use Esslassi\Metable\Metable;

class User extends Eloquent
{
    use Metable;
    ...
}

$user = new User;
$user->name = 'Esslassi Mohammed'; // model attribute
$user->country = 'Morocco'; // meta data attribute
$user->save(); // save model

$user = new User;
$user->name = 'Esslassi Mohammed';
$user->setMeta('country', 'Morocco');
$user->save();

$user = new User;
$user->name = 'Esslassi Mohammed';
$user->setMeta([
    'country' => 'Morocco',
    'city' => 'Fez'
    ...
]);
$user->save();

$user = new User;
$user->setAttributes([
    'name' => 'Esslassi Mohammed',
    'country' => 'Morocco',
    'city' => 'Fez'
    ...
]);
$user->save();

User::create([
    'name' => 'Esslassi Mohammed',
    'country' => 'Morocco',
    'city' => 'Fez'
    ...
]);

$user = User::find(1);
$user->name // model attribute
unset($user->country) // remove meta on save
$user->save();

$user = User::find(1);
$user->removeMeta('country');
$user->save();

$user = User::find(1);

$user->removeMeta(['country', 'city']);
OR
$user->removeMeta('country', 'city');

$user->save();

$user = User::find(1);
$user->deleteAllMeta();

...
if (isset($user->country)) {
    ...
}
...

...
if ($user->hasMeta('country')){

}
...

//By providing an array
$user->hasMeta(['country', 'city']);
// By pipe spliter
$user->hasMeta('country|city');
// By comma spliter
$user->hasMeta('country,city');

$user = User::find(1);
$user->removeMeta('country');

$user->hasMeta('country', true); // Return true

$user->hasMeta('country'); // Return false
...

$user = User::find(1);
$user->name; // Return Eloquent value
$user->country; // Return meta value

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

$user = $user->getMeta('country', 'Morocco');

// By comma
$user = $user->getMeta('country,city');
// By pipe
$user = $user->getMeta('country|city');
// By an array
$user = $user->getMeta(['country', 'city']);

protected $disableFluentMeta = true;

$user = User::find(1);
$user->country = 'Morocco';// This will not set meta, this action will call setAttribute() of model class.
$user->country;// Here will not retrieve meta
unset($user->country);// No action will take
isset($user->country);// Will not check if meta exists

$user = User::find(1);
$metas = $user->getAllMeta();

$users = User::whereMeta('country', '=', 'Morocco')
    ->get();

$users = User::whereMeta('country', 'Morocco')
    ->get();

$users = User::whereMeta('country', 'Morocco')
    orWhereMeta('continent', 'Africa')
    ->get();

$users = User::whereMeta('country', 'Morocco')
    ->orWhere(function (Builder $query) {
        $query->whereMeta('continent', 'Africa')
            ->whereMeta('competition', 'CAN');
    })
    ->get();

$users = User::whereMetaNot('country', 'Morocco')
    ->get();

$users = User::whereMetaIn('country', ['Morocco', 'Algeria', 'Egypt', 'Tunisia'])
    ->get();

$users = User::whereMetaNull('country')
    ->get();

$users = User::whereMetaNotNull('country')
    ->get();

$users = User::whereMetaNotNull('country')
    ->get();

$users = User::whereMetaDoesntHave('country')
    ->get();

$users = User::whereInMetaArray('countries', 'Morocco')
    ->get();

namespace App\Models;
 
use Illuminate\Database\Eloquent\Model;
use Esslassi\Metable\Relations\MetaOne;
 
class User extends Model
{
    /**
     * Get the car.
     */
    public function car(): MetaOne
    {
        return $this->hasMetaOne(Car::class, 'car_id');
    }
}

$user = User::find(1);
$car = $user->car;

class User extends Model
{
    /**
     * Get the car.
     */
    public function car(): MetaOne
    {
        return $this->hasMetaOne(
            Car::class,
            'car_id', // Meta key of the users meta...
            'id' // Local key on the users table...
        );
    }
}