🍔Create Metadata
Creating and adding metadata
Metadata supports all data types in PHP that can store null, empty, string, int, float and array
🔥 createMetadata
createMetadata
// Syntax
$model->createMetadata(array|Collection): \Waad\Metadata\Models\Metadata;
To create metadata you can use the createMetadata function, which is capable of handling various data types such as strings, integers, floats, arrays, and null values. Below is an example demonstrating how metadata can be structured and assigned to a company object:
Example Usage
// Usage array input
$model->createMetadata([
'language' => 'English', // string data type
'is_visible' => true, // boolean data type
'phone' => '', // empty string
'slug' => null, // null value
'theme' => 'dark', // string data type
'views' => 100, // integer data type
'rating' => 4.5, // float data type
'sports' => ['football', 'basketball'], // array data type
]);
// Usage Collection input
$model->createMetadata(collect(['language' => 'English', 'theme' => 'dark']));
metadata
array, Collection
🔥 createManyMetadata
// Syntax
$model->createManyMetadata(array|Collection $metadatas): Collection|false;
Example Usage
// Note: Metadata input must be nested array or collection
// Usage array input
$company->createManyMetadata([
[
'theme' => 'dark',
'sports' => ['football', 'basketball'],
],
[
'theme' => 'light',
'sports' => ['handball', 'golf'],
],
]);
// Usage Collection input
$model->createManyMetadata(collect(
[
[
'language' => 'English',
'theme' => 'dark'
],
[
'language' => 'Arabic',
'theme' => 'auto'
],
]
));
// metadata not nested array or collection
$model->->createManyMetadata([
'language' => 'Arabic',
'theme' => 'dark',
]); // return false
$model->->createManyMetadata(collect([
'language' => 'Arabic',
'theme' => 'dark',
])); // return false
// Usage empty Collection or array input
$model->createManyMetadata([]); // return false
$model->createManyMetadata([ [] ]); // return false
$model->createManyMetadata([ collect([]) ]); // return false
$model->createManyMetadata([ collect(['']) ]); // return false
metadatas
array, Collection
🔥 addKeysMetadataById
This method adds new values with keys if metadata exists.
// Syntax
// @param $id string
// @param $keys array|Collection|string|int|null
// @param $value array|Collection|string|int|float|bool|null
$model->addKeysMetadataById($id, $keys, $value = null): bool
Example Usage
// Use the method for multiple or individual keys
// ++++++++++ array Keys ++++++++++
$status = $model->addKeysMetadataById($id, [
'language' => 'English',
'is_visible' => true,
]);
// ++++++++++ Collection Keys ++++++++++
$status = $model->addKeysMetadataById($id, collect([
'language' => 'English',
'is_visible' => true,
]));
// ++++++++++ Individual Key ++++++++++
$model->addKeysMetadataById($id, keys: 'language', value: 'English');
$model->addKeysMetadataById($id, keys: 7, value: 'some value');
$model->addKeysMetadataById($id, 'tags', ['action', 'drama']);
$model->addKeysMetadataById($id, 'location', ['lat'=> 44.4324, 'lng' => 43.5346]);
$model->addKeysMetadataById($id, 'tags', collect(['action', 'drama']));
$model->addKeysMetadataById($id, 'phone', null);
$model->addKeysMetadataById($id, 'address', '');
$model->addKeysMetadataById($id, 'is_public', true);
$model->addKeysMetadataById($id, 'visits', 501);
$model->addKeysMetadataById($id, 'exchange', 7.35);
id
string
keys
array, Collection, string, int, null
value
array, Collection, string, int, float, bool, null
🔥 addKeyMetadataById
addKeyMetadataById
It's an Alias of the `addKeysMetadataById` method used for adding Individual Key
// Syntax
// @param $id string
// @param $key string|int|null
// @param $value array|Collection|string|int|float|bool|null
$model->addKeyMetadataById($id, $key, $value = null): bool
Example Usage
// Use the method for individual key
$model->addKeyMetadataById($id, key: 'language', value: 'English');
$model->addKeyMetadataById($id, key: 7, value: 'some value');
$model->addKeyMetadataById($id, key: 'length', value: '');
$model->addKeyMetadataById($id, key: 'length', value: null);
$model->addKeyMetadataById($id, key: 'rating', value: 5.4);
$model->addKeyMetadataById($id, key: 'length', value: 175);
$model->addKeyMetadataById($id, key: 'actors', value: ['john', 'smith']);
$model->addKeyMetadataById($id, 'location', ['lat'=> 44.4324, 'lng' => 43.5346]);
$model->addKeyMetadataById($id, key: 'actors', value: collect(['john', 'smith']));
id
string
key
string, int, null
value
array, Collection, string, int, float, bool, null
Last updated