PHP code example of danielefavi / laravel-metadata
1. Go to this page and download the library: Download danielefavi/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/ */
use DanieleFavi\Metadata\HasMetadata; // to add in your model
class User extends Authenticatable
{
use HasFactory;
use HasMetadata; // to add in your model
// ...
}
$user->saveMeta('phone_number', '111222333'); // storing a value
$user->saveMeta('color_preference', ['orange', 'yellow']); // storing an array
$phoneNumber = $user->getMeta('phone_number'); // the value of $phoneNumber is '111222333'
// Default value in case the metadata has not been found (default: null)
$anotherMeta = $user->getMeta('another_meta', 10);
// return an array key => value with the metadata specified for the given keys
$metas = $user->getMetas(['phone_number', 'address']);
// the value of the $metas is an array key => value:
// [
// 'phone_number' => '111222333',
// 'address' => '29 Arlington Avenue'
// ]
// return an array key => value containing all metadata of the user model
$metas = $user->getMetas();
$metaObj = $user->getMetaObj('address');
// delete a single metadata
$user->deleteMeta('address');
// delete multiple metadata
$user->deleteMeta(['phone_number', 'address']);
// delete all metadata
$user->deleteAllMeta();