2023-06-11 12:14:03 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Modules\Meta\Eloquent;
|
|
|
|
|
|
|
|
use Modules\Meta\Entities\MetaData;
|
2023-12-03 14:07:47 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
2023-06-11 12:14:03 +00:00
|
|
|
|
|
|
|
trait HasMetaData
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The "booting" method of the trait.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function bootHasMetaData()
|
|
|
|
{
|
|
|
|
static::saved(function ($entity) {
|
|
|
|
$entity->saveMetaData(request('meta', []));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-12-03 14:07:47 +00:00
|
|
|
|
2023-06-11 12:14:03 +00:00
|
|
|
/**
|
2023-12-03 14:07:47 +00:00
|
|
|
* Save metadata for the entity.
|
|
|
|
*
|
|
|
|
* @param array $data
|
2023-06-11 12:14:03 +00:00
|
|
|
*
|
2023-12-03 14:07:47 +00:00
|
|
|
* @return Model
|
2023-06-11 12:14:03 +00:00
|
|
|
*/
|
2023-12-03 14:07:47 +00:00
|
|
|
public function saveMetaData($data = [])
|
2023-06-11 12:14:03 +00:00
|
|
|
{
|
2023-12-03 14:07:47 +00:00
|
|
|
$this->meta->fill([locale() => $data])->save();
|
2023-06-11 12:14:03 +00:00
|
|
|
}
|
|
|
|
|
2023-12-03 14:07:47 +00:00
|
|
|
|
2023-06-11 12:14:03 +00:00
|
|
|
/**
|
2023-12-03 14:07:47 +00:00
|
|
|
* Get the meta for the entity.
|
2023-06-11 12:14:03 +00:00
|
|
|
*
|
2023-12-03 14:07:47 +00:00
|
|
|
* @return MorphToMany
|
2023-06-11 12:14:03 +00:00
|
|
|
*/
|
2023-12-03 14:07:47 +00:00
|
|
|
public function meta()
|
2023-06-11 12:14:03 +00:00
|
|
|
{
|
2023-12-03 14:07:47 +00:00
|
|
|
return $this->morphOne(MetaData::class, 'entity')->withDefault();
|
2023-06-11 12:14:03 +00:00
|
|
|
}
|
|
|
|
}
|