vortechron/laravel-meta

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

dev-master 2017-11-08 18:30 UTC

This package is auto-updated.

Last update: 2024-09-29 04:25:58 UTC


README

Laravel Source Build Status License

Metable 特性允许您将元数据访问能力添加到模型中,就像它是模型上的一个属性。Metable 是流畅的,就像使用 eloquent 模型属性一样,您可以设置或移除元数据。按照文档了解详细信息。

安装

Composer

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

"kodeine/laravel-meta": "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 Kodeine\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();