Use in Model
how to call trait inside model.
Setup
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Waad\Metadata\Traits\HasManyMetadata;
class Post extends Model
{
use HasManyMetadata; // ----> use this trait
// Enabled Append id with content metadata (default)
public $metadataNameIdEnabled = true;
// Custom Append key of id with metadata (default)
public $metadataNameId = 'id';
..........
}
Custom Append ID with Run Time
Using example Get Metadata
// Enabled Append Id true
$model->setMetadataNameIdEnabled(true)->getMetadata();
/** will return:
* [
* [
* 'id' => '01jj9s16xcze6byv94fv2f6gc5', // <<<---------
* 'theme' => 'dark',
* 'language' => 'Arabic',
* ],
* [
* 'id' => '01jj9rwvm0g3sypyyb91v5x2gz', // <<<---------
* 'theme' => 'light',
* 'language' => 'English',
* ],
* ]
*/
// Enabled Append Id false
$model->setMetadataNameIdEnabled(false)->getMetadata();
/** will return:
* [
* [
* 'theme' => 'dark',
* 'language' => 'Arabic',
* ]
* ]
*/
// Enabled Append Id true and changed name of id
$model->setMetadataNameIdEnabled(true)->setMetadataNameId('no')->getMetadata();
/** will return:
* [
* [
* 'no' => '01jj9s16xcze6byv94fv2f6gc5', // <<<---------
* 'theme' => 'dark',
* 'language' => 'Arabic',
* ]
* ]
*/
// Enabled Append Id false and changed name of id
$model->setMetadataNameIdEnabled(false)->setMetadataNameId('no')->getMetadata();
/** will return:
* [
* [
* 'theme' => 'dark',
* 'language' => 'Arabic',
* ]
* ]
*/
Last updated