PHP code example of nncodes / laravel-meta-attributes
1. Go to this page and download the library: Download nncodes/laravel-meta-attributes 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/ */
nncodes / laravel-meta-attributes example snippets
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Nncodes\MetaAttributes\Concerns\HasMetaAttributes;
class YourModel extends Model
{
use HasMetaAttributes;
}
$yourModel = YourModel::find(1);
/**
* Store the meta value using type auto discover
*
* @note types: boolean, integer, double, string,
* object, array, collection, date, datetime.
*/
$yourModel->setMeta($key)->value($value);
//For all other types you can specify using casting methods.
$yourModel->setMeta('departament')->asString('IT');
$yourModel->setMeta('salaryInteger')->asInteger(1489);
$yourModel->setMeta('salaryFloat')->asFloat(1489.909);
$yourModel->setMeta('salaryDecimal')->asDecimal(1489.9, $digitgs = 2);
$yourModel->setMeta('salaryDouble')->asDouble(1489.90);
$yourModel->setMeta('salaryPerSecond')->asReal(0.00001);
$yourModel->setMeta('favoriteColors')->asArray([
'red', 'blue', 'yellow', 'white'
]);
$yourModel->setMeta('tastes')->asCollection([
['name' => 'Coffee', 'type' => 'Beverage', 'rate' => 9],
['name' => 'Rice', 'type' => 'Food', 'rate' => 7],
]);
$yourModel->setMeta('skills')->asObject([
'PHP' => 'Very Good',
'Laravel' => 'Very Good',
'MySQL' => 'Good'
]);
$yourModel->setMeta('isAdult')->asBoolean(true);
$yourModel->setMeta('nid')->asEncrypted('FL-104050');
$yourModel->setMeta('birthdate')->asDate(
'1991-01-29',
$format = 'Y-m-d H:i:s'
);
$yourModel->setMeta('lastLoginAt')->asDatetime(
'2020-01-01 10:10:10',
$format = 'Y-m-d H:i:s'
);
$yourModel->setMeta('createdAt')->asTimestamp($timestamp = time());
$yourModel->getMeta($key); //Eloquent object
$yourModel->getMetaValue($key, $fallback = null); //Only the value