mphpmaster/laravel-meta

为 Eloquent 模型提供流畅的元数据,就像模型上的属性一样。hlaCk 版本

0.0.3 2020-11-12 10:11 UTC

This package is auto-updated.

Last update: 2024-09-12 18:11:36 UTC


README

Laravel Source Build Status License

Metable 特性允许将元数据作为模型属性访问。Metable 是流畅的,就像使用 eloquent 模型属性一样,您可以设置或取消设置元数据。根据文档了解更多信息。

安装

Composer

将以下内容添加到您的 composer.json 文件中的 require 对象

"mphpmaster/laravel-meta": "master"

之后,运行 composer install 安装此包。

迁移表模式

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
    Schema::create('posts_meta', function (Blueprint $table) {
        $table->increments('id');

        $table->integer('post_id')->unsigned()->index();
        $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');

        $table->string('type')->default('null');

        $table->string('key')->index();
        $table->text('value')->nullable();

        $table->timestamps();
    });
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
    Schema::drop('posts_meta');
}

配置

模型设置

接下来,将 Metable 特性添加到每个可测量模型定义中

use mPhpMaster\LaravelMeta\Metable;

class Post extends Eloquent
{
    use Metable;
}

Metable 特性将自动根据您的模型名称设置元数据表。默认的元数据表名称为 model_meta。如果您需要定义自己的元数据表名称,可以在模型中指定

class Post extends Eloquent
{
    protected $metaTable = 'posts_meta'; //optional.
}

默认模型属性值

此外,您可以通过在模型上设置名为 $defaultMetaValues 的数组来设置默认值。设置默认值有两个副作用

  1. 如果元数据属性不存在,将返回默认值而不是 null

  2. 如果您尝试将元数据属性设置为默认值,元数据表中的行将被删除,这将导致返回默认值,按照规则 1。

这对于大多数项目来说是一个期望和预期的功能,但请注意,您可能需要根据需要重新实现自己的自定义访问器和修改器。

此功能最适合记录规则异常的元数据条目。例如:员工因生病请假(默认值:在办公室),节点因维护而关闭(默认值:节点开启)等。这意味着表不需要存储每个预期状态的数据,只需存储处于异常状态的数据,并在创建时允许行具有默认状态,而无需添加写入代码。

   public $defaultMetaValues = [
      'is_user_home_sick' => false,
   ];

注意事项

当您扩展模型并希望使用相同的元数据表时,必须重写 getMetaKeyName 函数。

class Post extends Eloquent
{

}

class Slideshow extends Post
{
    protected function getMetaKeyName()
    {
        return 'post_id' // The parent foreign key
    }   
}

处理元数据

设置内容元数据

要对现有内容或创建新数据设置元数据

流畅方式,您可以在常规 eloquent 模型上无缝设置元数据。

$post = Post::find(1);
$post->name = 'hello world'; // model attribute
$post->content = 'some content goes here'; // meta data attribute
$post->save(); // save attributes to respective tables

或者

$post = Post::find(1);
$post->name = 'hello world'; // model attribute
$post->setMeta('content', 'Some content here');
$post->save();

或者一次设置多个元数据

...
$post->setMeta([
    'content' => 'Some content here',
    'views' => 1,
]);
$post->save();

或者一次设置多个元数据和列

...
$post->setAttributes([
    'name' => 'hello world'; // model attribute
    'content' => 'Some content here',
    'views' => 1,
]);
$post->save();

注意:如果内容已有元数据,现有值将被更新。

取消设置内容元数据

同样,您可以从现有内容中取消设置元数据

流畅方式取消设置。

$post = Post::find(1);
$post->name // model attribute
unset($post->content) // delete meta on save
$post->save();

或者

$post->unsetMeta('content');
$post->save();

或者一次取消多个元数据

$post->unsetMeta('content,views');
// or
$post->unsetMeta('content|views');
// or
$post->unsetMeta('content', 'views');
// or array
$post->unsetMeta(['content', 'views']);

$post->save();

注意:如果内容没有请求的元数据,系统不会抛出错误。

检查元数据

查看内容是否具有元数据

流畅方式,Metable 足够聪明,能够理解 $post->content 是元数据属性。

if (isset($post->content)) {

}

检索元数据

要检索内容上的元数据值,请使用 getMeta 方法

流畅方式,您可以像访问模型属性一样访问元数据。就像您在常规eloquent模型中所做的那样。

$post = Post::find(1);
dump($post->name);
dump($post->content); // will access meta.

或者

$post = $post->getMeta('content');

或者指定一个默认值,如果未设置

$post = $post->getMeta('content', 'Something');

您还可以一次性检索多个元数据,并获取一个清晰的集合

// using comma or pipe
$post = $post->getMeta('content|views');
// or an array
$post = $post->getMeta(['content', 'views']);

检索所有元数据

要获取与某条内容关联的所有元数据,使用不带任何参数的 getMeta

$metas = $post->getMeta();

检索所有元数据数组

要获取与某条内容关联的所有元数据并以数组形式返回,请使用 toArray 方法

$metas = $post->getMeta()->toArray();

元数据表连接

当您需要根据元数据过滤模型时,可以在Eloquent查询构建器中使用 meta 范围。

$post = Post::meta()
    ->where(function($query){
          $query->where('posts_meta.key', '=', 'revision')
                ->where('posts_meta.value', '=', 'draft');
    })

预加载

当您需要从模型中检索多个结果时,您可以预加载 metas

$post = Post::with(['metas'])->get();

防止元数据属性被填充

当您将模型转换为数组(或JSON)且不需要所有元数据字段时,您可以创建一个模型的属性以防止元数据被添加到结果数组中。您也可以在eloquent关系中使用它。

/* Post model */
public $hideMeta = true; // Do not add metas to array