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
まず、モデルで 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
1 つのモデルに属性値があることを確認したい場合は、 hasAttributeValue
メソッドを使用できます。
if ( $ product -> hasAttributeValue ( ' 17 ' )) {
return ' attribute value ' ;
}
return ' no attribute value ' ;
1 つのモデルに属性タイトルがあることを確認したい場合は、 hasAttributeTitle
メソッドを使用できます。
if ( $ product -> hasAttributeTitle ( ' milwad ' )) {
return ' attribute title ' ;
}
return ' no attribute title ' ;
1 つのモデルのすべての属性を削除したい場合は、 deleteAllAttribute
メソッドを使用できます。
$ product -> deleteAllAttribute ();
モデルの特定の属性を削除したい場合は、 deleteAttribute
メソッドを使用できます。
$ product -> deleteAttribute ( ' title ' , ' value ' );
タイトルごとに特定の属性を削除したい場合は、 deleteAttributeByTitle
メソッドを使用できます。
同じタイトルの属性が 2 つある可能性があります。この方法で削除すると、2 つの属性が削除されます。
$ product -> deleteAttributeByTitle ( ' title ' );
値によって特定の属性を削除したい場合は、 deleteAttributeByValue
メソッドを使用できます。
同じ値を持つ 2 つの属性がある可能性があります。この方法で削除すると、2 つの属性が削除されます。
$ product -> deleteAttributeByValue ( ' value ' );
以下を使用してテストを実行します。
vendor/bin/pest
composer test
composer test-coverage
移行テーブル名を変更するか、デフォルトのモデルを変更したい場合は、 config
フォルダーに存在するlaravel-attributes
config を使用できます。
<?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