necenzurat/eloquent-meta

Eloquent 模型流畅元数据,就像它是模型的一个属性。

1.0.5 2021-01-23 07:50 UTC

This package is auto-updated.

Last update: 2024-09-23 15:05:20 UTC


README

Laravel Latest Version Downloads Source SensioLabsInsight License BADGINATOR

kodeine/laravel-meta 分支更新,与 Laravel 5.x(包括 5.5)兼容

Metable 特性添加了将元数据作为模型属性访问的能力。Metable 是流畅的,就像使用 eloquent 模型属性设置或取消设置元数据一样。遵循文档了解更多信息。

安装

Composer

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

composer require necenzurat/eloquent-meta

之后,运行 composer install 安装包。

待办事项:生成器

迁移表模式

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

        $table->integer('model_id')->unsigned()->index();
        $table->foreign('model_id')->references('id')->on('MODEL')->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('MODEL_meta');
}

配置

模型设置

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

use necenzurat\EloquentMeta\Metable;

class Post extends Eloquent
{
    use Metable;
}

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

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

注意事项

当你扩展模型并仍然想使用相同的元数据表时,必须重写 getMetaKeyName 函数。

class Post extends Eloquent
{
    
}

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

使用元数据

设置内容元数据

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

流畅方式,你可以像在常规 eloquent 模型上设置元数据一样完美地设置元数据。Metable 检查属性是否属于模型,如果不是,它将访问元数据模型来追加或设置新的元数据。

$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 = 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');

您还可以一次性检索多个元数据并获取一个 Illuminate 集合

// 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(['meta'])->get();