skeemer/laravel-meta

此包已被废弃且不再维护。未建议替代包。

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

v1.0 2017-01-11 05:25 UTC

This package is not auto-updated.

Last update: 2023-08-05 04:52:25 UTC


README

Laravel License

Metable特性可以为你的模型添加访问元数据的能力,就像它是一个属性一样。Metable是流畅的,就像使用Eloquent模型属性一样,你可以设置或取消设置元数据。请按照文档操作以了解更多信息。这是从kodeine/laravel-meta分支出来的,修改后可以在没有Laravel应用的情况下运行,并将元数据扁平化到模型中。

安装

Composer

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

"skeemer/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');

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

// 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');
    })