laravel-attributes
套件是一個旨在幫助 Laravel 開發人員輕鬆管理和在專案中實現自訂屬性的工具。它使您能夠直接在 Eloquent 模型上定義屬性,從而更輕鬆地處理動態或計算屬性,而無需直接修改資料庫模式。使用此套件,您可以為模型建立和配置屬性集,從而更輕鬆地組織和擴展 Laravel 應用程式中的資料處理。它對於需要可自訂和靈活的資料模型的專案特別有用。
你對屬性沒有任何壓力!您可以為任何模型建立屬性並像喝水一樣顯示:)
PHP: ^8.0
Laravel Framework: ^9.0
屬性 | L9 | L10 |
---|---|---|
1.0 | ✅ | ✅ |
composer require milwad/laravel-attributes
發布設定檔後。
php artisan vendor:publish --provider= " MilwadLaravelAttributesLaravelAttributesServiceProvider "
發布後,您可以遷移遷移檔案。
php artisan migrate
首先,您在模型中使用特徵。
<?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,
];
這個項目的存在要感謝所有做出貢獻的人。貢獻
如果您發現有關安全的錯誤,請發送郵件至 [email protected],而不是使用問題追蹤器。
如果這個套餐對你有幫助的話,可以買杯咖啡:)❤️
0xf208a562c5a93DEf8450b656c3dbc1d0a53BDE58