dhcmega / laravel-meta
从 kodeine/laravel-meta 分支而来。Eloquent 模型流畅元数据,就像模型上的属性。
Requires
- php: >=5.4.0
- illuminate/support: ~5.0|~5.1|^6.0
This package is auto-updated.
Last update: 2024-09-07 00:27:34 UTC
README
Metable 特性允许您像访问模型属性一样访问元数据。Metable 是流畅的,就像使用 eloquent 模型属性一样,您可以设置或取消设置元数据。按照文档了解更多信息。
安装
Composer
将以下内容添加到您的 composer.json 文件中的 require 对象
"dhcmega/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 Dhcmega\Metable\Metable; class Post extends Eloquent { use Metable; }
Metable 特性将自动根据您的模型名称设置元数据表。默认的元数据表名称为 model_meta
。如果您需要定义自己的元数据表名称,可以在模型中指定
class Post extends Eloquent { protected $metaTable = 'posts_meta'; //optional. }
默认模型属性值
此外,您还可以通过在模型上设置名为 $defaultMetaValues
的数组来设置默认值。设置默认值有两个副作用
-
如果元数据属性不存在,将返回默认值而不是
null
。 -
如果您尝试将元数据属性设置为默认值,则元数据表中的行将被删除,这将导致返回默认值,按照规则 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 模型上无缝设置元数据,就像设置元数据一样。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->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