agpretto / wing
给你的优雅模型添加翅膀
dev-master
2019-09-18 14:09 UTC
Requires (Dev)
- mockery/mockery: ^1.0
- orchestra/testbench: ^4.0
- phpunit/phpunit: ^7.5|^8.0
This package is auto-updated.
Last update: 2024-09-19 01:07:07 UTC
README
此包为工作进度(WIP),请在稳定发布之前不要使用。
给你的优雅模型添加翅膀。
此包允许你通过一组JSON元数据扩展你的模型。
配置
Composer
在您的composer.json文件中要求此包
"agpretto/wing": "dev-master"
安装
此包解决了向模型添加元数据描述的问题。您可以为单个模型分配任意多个值来描述与模型实例相关的所有内容。
-
使用
wing:install
命令安装此包。 -
您只需要在您的模型之一中使用
HasWing
特性。
namespace App; use Illuminate\Database\Eloquent\Model; use Agpretto\Wing\HasWing; class Article extends Model { use HasWing; }
用法
安装此包后,您可以为模型使用Wing的强大功能。我们使用文章模型实体的一个实例来展示此包的方法。
// - grab an article $article = Article::findOrFail(1);
简单数据
假设我们想为我们的Article
实例扩展一个新的简单数据翅膀值。
// add one simple value $article->addWing('foo'); // extend the model with a wing // get the value back $value = $article->metadata(); // foo
结构化数据
现在假设我们想为我们的Article
实例扩展结构化数据值。
// structured data $data = [ 'foo' => 'bar', 'bar' => 'baz', 'min' => 'max', ]; $article->addWing($data); // extend the model with a wing // get the values back $data = $article->metadata(); // stdClass $data->foo; // bar $data->bar; // baz $data->min; // max // directly access the data $article->metadata()->foo; // bar $article->metadata()->bar; // baz $article->metadata()->min; // max
复杂结构化数据
请谨慎使用复杂结构!
现在假设我们想为我们的Article
实例扩展复杂的数据结构。
// structured data $data = [ 'foo' => [ 'foo' => 'bar', 'bar' => 'baz' ], 'bar' => 'baz', 'a-strange-key' => [ 'only', 'value' ] ]; $article->addWing($data); // extend the model with a wing // again, we can get the values back $data = $article->metadata(); // stdClass $data->foo; // stdClass $data->foo->foo; // bar $data->bar; // baz $data->{'a-strange-key'}; // stdClass $data->{'a-strange-key'}[0]; // only // directly access the data $article->metadata()->foo; // stdClass $article->metadata()->foo->foo; // bar $article->metadata()->bar; // baz $article->metadata()->{'a-strange-key'}; // stdClass $article->metadata()->{'a-strange-key'}[0]; // only
检查数据
您可以使用hasMetdata
方法检查您的结构化数据中是否存在数据。
// structured data $data = [ 'foo' => 'bar', 'bar' => 'baz', 'min' => 'max', ]; $article->addWing($data); // extend the model with a wing $article->hasMetadata('foo'); // true $article->hasMetadata('max'); // false
更新翅膀内的数据
因为Wing使用JSON数据字段在数据库中存储您的模型结构化数据,所以您可以使用JSON表示法更新翅膀关系的属性。
您可以在如何使用JSON数据类型找到大量示例。
此包为您提供了两个简化方法来更新Wing的数据。
更新简单结构化数据
使用updateData
方法更新一个简单结构。
// structured data $data = [ 'foo' => 'bar', 'bar' => 'baz', 'min' => 'max', ]; $article->addWing($data); // extend the model with a wing $article->metadata()->foo; // bar // update wing data $article->updateWing('foo', 'bong'); $article->refresh(); // reload model relations $article->metadata()->foo; // bong
更新复杂数据结构的部分
使用updatePartOfWing
遍历您的数据并更新它们的一部分。
此方法简化了对数据结构的访问。
// structured data $data = [ 'foo' => [ 'foo' => 'bar', 'bar' => 'baz' ], 'bar' => 'baz', 'a-strange-key' => [ 'only', 'value' ] ]; $article->addWing($data); // extend the model with a wing //update data using a path $article->updatePartOfWing('foo->foo', 'bong'); $article->refresh(); // reload model relations $article->metadata()->foo->foo; // bong
致谢
此包由星际开发者Andrea Giuseppe为您构建 - Github