ewereka/laravel-metable

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

v1.0.0 2019-09-07 01:57 UTC

This package is auto-updated.

Last update: 2024-09-07 13:01:18 UTC


README

Laravel Source License

Metable 特性允许将元数据作为属性访问,就像在模型上一样。Metable 是流畅的,就像使用 eloquent 模型属性一样,您可以设置或删除元数据。按照文档说明了解更多。

安装

Composer

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

"ewereka/laravel-metable": "dev-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 Ewereka\Metable\Metable;

class Post extends Eloquent
{
    use Metable;
}

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

class Post extends Eloquent
{
    protected $metaTable = 'posts_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(['metas'])->get();