PHP code example of vkovic / laravel-model-meta

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

    

vkovic / laravel-model-meta example snippets


namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Vkovic\LaravelModelMeta\Models\Traits\HasMetadata;

class User extends Authenticatable
{
    use Notifiable, HasMetadata; // <= trait is added here

    // ...
}

$user = User::inRandomOrder()->first();

// Set meta value as string
$user->setMeta('foo', 'bar');

// Get meta value
$user->getMeta('foo'); // : 'bar'

// In case there is no metadata found for given key,
// we can pass default value to return
$user->getMeta('baz', 'default'); // : 'default'

$user->setMeta('age', 35);
$user->setMeta('temperature', 24.7);
$user->setMeta('value', null);
$user->setMeta('employed', true);
$user->setMeta('fruits', ['orange', 'apple']);

$user->getMeta('age'); // : 35
$user->getMeta('temperature'); // : 24.7
$user->getMeta('value'); // : null
$user->getMeta('employed'); // : true
$user->getMeta('fruits'); // : ['orange', 'apple']

$user->setMeta('foo', 'bar');

$user->metaExists('foo'); // : true

$user->setMeta('a', 'one');
$user->setMeta('b', 'two');

$user->countMeta(); // : 2

$user->setMeta('a', 'one');
$user->setMeta('b', 'two');
$user->setMeta('c', 'three');

// Get all metadata
$user->allMeta(); // : ['a' => 'one', 'b' => 'two', 'c' => 'three']

// Get only keys
$user->metaKeys(); // : [0 => 'a', 1 => 'b', 2 => 'c']

$user->setMeta('a', 'one');
$user->setMeta('b', 'two');
$user->setMeta('c', 'three');

// Remove meta by key
$user->removeMeta('a');

// Or array of keys
$user->removeMeta(['b', 'c']);

$user->purgeMeta();

$user->setMeta('age', 35);

// Equals operator
User::whereMeta('age', '=', 35)->get();
// or shorther
User::whereMeta('age', 35)->get();

// Comparison operators
User::whereMeta('age', '>', 18)->get();
User::whereMeta('age', '!=', 20)->get();
// or with other comparison operators (<, <=, >, >=, =, <>, !=)

// All of the examples above will return Collection of users which meet's criteria,
// in this case our $user

$user->setMeta('company', 'Acme');
$anotherUser->setMeta('role', 'admin');

// Meta key
User::whereHasMetaKey('manager')->get();

// Array of keys
User::whereHasMetaKey(['company', 'role'])->get();

// All of the examples above will return Collection of users which meet's criteria,
// in this case our $user and $anotherUser

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Vkovic\LaravelModelMeta\Models\Interfaces\HasMetadataInterface;

class User extends Authenticatable implements HasMetadataInterface // <= interface is added here
{
    // ...
}

if ($model instanceof HasMetadataInterface) {
    // ... do something
}
bash
php artisan migrate