PHP code example of astritzeqiri / laravel-metadata

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

    

astritzeqiri / laravel-metadata example snippets


'providers' => array(
    // ...
    AstritZeqiri\Metadata\LaravelMetaDataServiceProvider::class
)

'aliases' => array(
    // ...
    'MetaData' => AstritZeqiri\Metadata\Models\MetaDada::class,
)

// E.x. User.php
// add this before the class declaration
use AstritZeqiri\Metadata\Traits\HasManyMetaDataTrait;

// after the class declaration add this code snippet:
use HasManyMetaDataTrait;

// get the instance
$user = \App\User::first();

// update a metadata if it exists else add a new one
$user->update_meta("meta_key", "meta_value");



// get the instance
$user = \App\User::first();

// get a metadata object with a given key
$user->get_meta("meta_key");

// if the second parameter is true it returns only the value
$user->get_meta("meta_key", true);


// get the instance
$user = \App\User::first();

// delete a metadata entry with a given key
$user->delete_meta("meta_key");

// delete all metadatas of a user
$user->delete_all_metas();




// Search by only one meta data.
$users = \App\User::metaQuery('hair_color', 'red')->get();
// filter the users that have red hair color

// Search by many meta data.
$users = \App\User::metaQuery(array(
	array('key' => 'hair_color', 'value' => 'red'),
	array('key' => 'phone_number', 'value' => '%111%', 'compare' => "LIKE")
), "OR")->get();
// filter the users that have red hair color
// or that their phone_number contains '111'


$ php artisan vendor:publish --provider="AstritZeqiri\Metadata\LaravelMetaDataServiceProvider"

$ php artisan migrate