Basics HasOneMetadata Create MetadataCreating and adding metadata has one morph relationship (one object)
Metadata supports all data types in PHP that can store null, empty, string, int, float and array
🔥 createMetadata
Copy // 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
Copy $company -> 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
] ) ;
// return Metadata Model or null
Return Type ⇒ \Waad\Metadata\Models\Metadata , null
🔥 addKeysMetadata
This method adds new metadata if it does not exist or adds new values with keys if metadata exists.
Copy // Syntax
// @param $keys array|Collection|string|int|null
// @param $value array|Collection|string|int|float|bool|null
$model -> addKeysMetadata ( $keys , $value = null ) : bool
Example Usage addKeysMetadata
Copy
// Method: addKeysMetadata
// The addKeysMetadata method returns a boolean value.
// Use `addKeysMetadata` method for multiple or individual keys
// ++++++++++ array Keys ++++++++++
$status = $company -> addKeysMetadata ( [
'language' => 'English' ,
'is_visible' => true ,
] ) ;
// ++++++++++ Collection Keys ++++++++++
$status = $company -> addKeysMetadata ( collect ( [
'language' => 'English' ,
'is_visible' => true ,
] )) ;
// ++++++++++ Individual Key ++++++++++
$status = $company -> addKeysMetadata ( keys : 'language' , value : 'English' ) ;
$status = $company -> addKeysMetadata ( keys : 7 , value : 'some value' ) ;
$status = $company -> addKeysMetadata ( 'tags' , [ 'action' , 'drama' ] ) ;
$status = $company -> addKeysMetadata ( 'location' , [ 'lat' => 44 . 4324 , 'lng' => 43 . 5346 ] ) ;
$status = $company -> addKeysMetadata ( 'tags' , collect ( [ 'action' , 'drama' ] )) ;
$status = $company -> addKeysMetadata ( 'phone' , null ) ;
$status = $company -> addKeysMetadata ( 'address' , '' ) ;
$status = $company -> addKeysMetadata ( 'is_public' , true ) ;
$status = $company -> addKeysMetadata ( 'visits' , 501 ) ;
$status = $company -> addKeysMetadata ( 'exchange' , 7 . 35 ) ;
Parameters Method
Accept Data Type
array, Collection, string, int, null
array, Collection, string, int, float, bool, null
🔥 addKeyMetadata
It's Alias of the ` addKeysMetadata ` method used for adding Individual Key
Copy // Syntax
// @param $key string|int|null
// @param $value array|Collection|string|int|float|bool|null
$model -> addKeyMetadata ( $key , $value = null ) : bool
Example Usage addKeyMetadata
Copy
// Use `addKeyMetadata` method for individual key
$status = $company -> addKeyMetadata ( key : 'language' , value : 'English' ) ;
$status = $company -> addKeyMetadata ( key : 7 , value : 'some value' ) ;
$status = $company -> addKeyMetadata ( key : 'length' , value : '' ) ;
$status = $company -> addKeyMetadata ( key : 'length' , value : null ) ;
$status = $company -> addKeyMetadata ( key : 'rating' , value : 5 . 4 ) ;
$status = $company -> addKeyMetadata ( key : 'length' , value : 175 ) ;
$status = $company -> addKeyMetadata ( key : 'actors' , value : [ 'john' , 'smith' ] ) ;
$status = $company -> addKeyMetadata ( 'location' , [ 'lat' => 44 . 4324 , 'lng' => 43 . 5346 ] ) ;
$status = $company -> addKeyMetadata ( key : 'actors' , value : collect ( [ 'john' , 'smith' ] )) ;
Parameters Method
Accept Data Type
array, Collection, string, int, float, bool, null