laravel attributes

其他类别 2025-08-20

laravel attributes


laravel-attributes软件包是一种工具,旨在帮助Laravel开发人员轻松管理和实现其项目中的自定义属性。它使您能够直接在雄辩的模型上定义属性,从而在不直接修改数据库架构的情况下处理动态或计算的属性变得更加简单。使用此软件包,您可以为模型创建和配置属性集,从而更容易组织和扩展Laravel应用程序中的数据处理。这对于需要可自定义和灵活的数据模型的项目特别有用。
您对属性没有任何压力!您可以为任何型号创建属性,并像饮料水一样显示:)

要求


  • PHP: ^8.0
  • Laravel Framework: ^9.0
属性 L9 L10
1.0
1.1

安装


 composer require milwad/laravel-attributes

发布配置文件后。

php artisan vendor:publish --provider= " MilwadLaravelAttributesLaravelAttributesServiceProvider "

发布后,您将迁移迁移文件。

 php artisan migrate

用法

首先,您在模型中使用特质。

 

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方法采用titlevalue

laravel attributes', ]); $product->attachAttribute('age', '17');">
 $ 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配置。

 

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许可发布。

贡献

由于所有贡献的人,该项目的存在。贡献

安全

如果您找到了有关安全性的错误,请邮件milwad.dev@gmail.com,而不是使用问题跟踪器。

如果此包装对您有帮助,您可以为我购买咖啡:)❤️

  • 伊拉南门户:https://daramet.com/milwad_khosravi
  • 贝宝门户:即将
  • metamask地址: 0xf208a562c5a93DEf8450b656c3dbc1d0a53BDE58
下载源码

通过命令行克隆项目:

git clone https://github.com/milwad-dev/laravel-attributes.git