🔥 getMetadata
Get all metadata records associated with the model as an array
// Syntax
$model->getMetadata(): array
Example Usage
// If metadataNameIdEnabled is true, each metadata record will include its ID
class Post extends Model
{
use HasManyMetadata;
public $metadataNameIdEnabled = true;
public $metadataNameId = 'id';
...........
}
$post->getMetadata();
/** will return:
* [
* [
* 'id' => '01jj9s16xcze6byv94fv2f6gc5', // <<<---------
* 'theme' => 'dark',
* 'language' => 'Arabic',
* ],
* [
* 'id' => '01jj9rwvm0g3sypyyb91v5x2gz', // <<<---------
* 'theme' => 'light',
* 'language' => 'English',
* ],
* ]
*/
🔥 getMetadataCollection
It's an Alias getMetadata
but return as Collection
// Syntax
$model->getMetadataCollection(): Collection
🔥 getMetadataById
Get a specific metadata record using its ID.
Retrieves metadata values from the metadata column. If keys are provided only the specified keys will be returned.
// Syntax
// @param $id string
// @param $keys array|Collection|string|int|null
$model->getMetadataById($id, $keys = null): array
Example Usage Without Keys Parameter
// Create Metadata
$model->createMetadata([
'language' => 'English',
'is_visible' => true,
'theme' => 'dark',
'year' => 2019,
'rating' => 4.5,
'tags' => ['action', 'drama'], // array data type
]);
// Using Without keys Parameter
$model->getMetadataById($id);
/**
* Will return array:
* [
* 'language' => 'English',
* 'is_visible' => true,
* 'theme' => 'dark',
* 'year' => 2019,
* 'rating' => 4.5,
* 'tags' => [
* 0 => 'action',
* 1 => 'drama',
* ]
* ]
*/
Example Usage With Keys Parameter
// +++++++++++ Using Keys array, Collection +++++++++++
$model->getMetadataById($id, ['theme', 'language']);
$model->getMetadataById($id, collect(['theme', 'language']));
/**
* Will return array:
* [
* 'theme' => 'dark',
* 'language' => 'English',
* ]
*/
// +++++++++++ Using Individual Key +++++++++++
$model->getMetadataById($id, 'theme');
/**
* Will return array:
* [
* 'theme' => 'dark',
* ]
*/
$model->getMetadataById($id, 'tags');
/**
* Will return array:
* [
* 'tags' => [
* 0 => 'action',
* 1 => 'drama',
* ]
* ]
*/
$model->getMetadataById($id, 3); // if metadata saved list array
$model->getMetadataById($id , null); // will return all content metadata as array
array, Collection, string, int, null
🔥 getKeyMetadataById
Get a single value from a metadata record by its ID and key.
// Syntax
/*
* @param $id string
* @param $key string|int
*/
$model->getKeyMetadataById($id, $key): string|int|float|bool|array|null
Example Usage
$model->getKeyMetadataById($id, 'language'); // return: 'English'
$model->getKeyMetadataById($id, 'is_visible'); // return: true
$model->getKeyMetadataById($id, 'theme'); // return: 'dark'
$model->getKeyMetadataById($id, 'year'); // return: 2019
$model->getKeyMetadataById($id, 'rating'); // return: 4.5
$model->getKeyMetadataById($id, 'not_exists'); // return null
$model->getKeyMetadataById($id, 3); // return (4th index) item in list array
$model->getKeyMetadataById($id, 'tags');
/**
* Will return:
* [
* 0 => 'action',
* 1 => 'drama',
* ]
*/
Return Type ⇒ array, string, int, float, bool, null