milwad / laravel-attributes
轻松创建属性
Requires
- php: ^8.0
- laravel/framework: ^9.0|^10.0|^11.0
Requires (Dev)
- orchestra/testbench: ^7.0|^8.0|^9.0
- pestphp/pest-plugin-laravel: ^1.4.0|^2.0.0
README
Laravel attributes 是一个易于创建属性的包。
使用 laravel attributes,您可以创建所有模型(多态)的属性。
您无需为属性感到压力!您可以创建任何模型的属性并像喝水一样显示 :)
要求
PHP: ^8.0
Laravel 框架: ^9.0
安装
composer require milwad/laravel-attributes
发布配置文件后。
php artisan vendor:publish --provider="Milwad\LaravelAttributes\LaravelAttributesServiceProvider"
发布后,您需要迁移迁移文件。
php artisan migrate
使用方法
首先,在模型中使用 trait。
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Milwad\LaravelAttributes\Traits\Attributable; class Product extends Model { use HasFactory, Attributable; }
之后,您将可以访问 attributes
关联等...
保存属性
如果您想将属性附加到模型,您可以使用 attachAttribute
方法。
attachAttribute
方法接受一个 title
和 value
。
$product = Product::query()->create([ 'name' => 'milwad', 'content' => 'laravel attributes', ]); $product->attachAttribute('age', '17');
保存多个属性
如果您有多个属性,您可以使用 attachAttributes
方法为模型保存属性。
$product = Product::query()->create([ 'name' => 'milwad', 'content' => 'text', ]); $data = [ [ 'title' => 'milwad', 'value' => 'developer', ], [ 'title' => 'milwad2', 'value' => 'developer2', ], [ 'title' => 'milwad3', 'value' => 'developer3', ], [ 'title' => 'milwad4', 'value' => 'developer4', ], [ 'title' => 'milwad5', 'value' => 'developer5', ], [ 'title' => 'milwad6', 'value' => 'developer6', ], ]; $product->attachAttributes($data);
通过查询获取属性
如果您想从关联中检索属性,您可以使用 attributes
。
$product = Product::query()->with('attributes')->get(); $product->attributes
检查属性值是否存在
也许您想检查一个模型是否具有某个属性值,您可以使用 hasAttributeValue
方法。
if ($product->hasAttributeValue('17')) { return 'attribute value'; } return 'no attribute value';
检查属性值是否存在
也许您想检查一个模型是否具有某个属性标题,您可以使用 hasAttributeTitle
方法。
if ($product->hasAttributeTitle('milwad')) { return 'attribute title'; } return 'no attribute title';
删除所有属性
如果您想删除一个模型的全部属性,您可以使用 deleteAllAttribute
方法。
$product->deleteAllAttribute();
删除特定属性
如果您想删除一个模型的特定属性,您可以使用 deleteAttribute
方法。
$product->deleteAttribute('title', 'value');
通过标题删除特定属性
如果您想通过标题删除特定属性,您可以使用 deleteAttributeByTitle
方法。
也许您有两个具有相同标题的属性,如果您使用此方法删除,将删除两个属性
$product->deleteAttributeByTitle('title');
通过值删除特定属性
如果您想通过值删除特定属性,您可以使用 deleteAttributeByValue
方法。
也许您有两个具有相同值的属性,如果您使用此方法删除,将删除两个属性
$product->deleteAttributeByValue('value');
测试
运行测试
vendor/bin/pest
composer test
composer test-coverage
自定义
如果您想更改迁移表名或更改默认模型,您可以使用位于 config
文件夹中的 laravel-attributes
配置。
<?php return [ /* * Table config * * Here it's a config of migrations. */ 'tables' => [ /* * Get table name of migration. */ 'name' => 'attributes', /* * Use uuid as primary key. */ 'uuids' => false, // Also in beta !!! ], /* * Model class name for attributes table. */ 'attributes_model' => \Milwad\LaravelAttributes\Attribute::class, ];
许可证
- 此包由 Milwad Khosravi 为 Laravel >= 9 创建和修改,并使用 MIT 许可证发布。
贡献
该项目的存在归功于所有贡献者。 CONTRIBUTING
安全性
如果您发现有关安全性的错误,请通过电子邮件发送到 milwad.dev@gmail.com,而不是使用问题跟踪器。