fafedaljghine/laravel-extra-field

一个允许为 Eloquent 模型分配额外字段的包

1.0.3 2022-06-24 15:50 UTC

This package is auto-updated.

Last update: 2024-09-24 20:51:49 UTC


README

Laravel 9.0 Software License

一个允许为 Eloquent 模型分配额外字段的包

联系我

您可以通过查看我的网站来检查我所有的信息。

安装

您可以通过 composer 安装此包

composer require fahedaljghine/laravel-extra-field

该包将自动注册自己。

您必须使用以下命令发布迁移

php artisan vendor:publish --provider="Fahedaljghine\ExtraField\ExtraFieldServiceProvider" --tag="migrations"

迁移 extras & extra_values

php artisan migrate

可选地,您可以使用以下命令发布配置文件

php artisan vendor:publish --provider="Fahedaljghine\ExtraField\ExtraFieldServiceProvider" --tag="config"

这是将发布到 config/extra-field.php 的文件内容

return [

    /*
     * The class name of the extra model that holds all extras.
     *
     * The model must be or extend `Fahedaljghine\ExtraField\Extra`.
     */
    'extra_model' => Fahedaljghine\ExtraField\Extra::class,

    /*
     * The class name of the extra value model that holds all values.
     *
     * The model must be or extend `Fahedaljghine\ExtraField\ExtraValue`.
     */
    'extra_value_model' => Fahedaljghine\ExtraField\ExtraValue::class,

    /*
     * The name of the column which holds the ID of the model related to the extra values.
     *
     * You can change this value if you have set a different name in the migration for the extra_values table.
     */
    'model_primary_key_attribute' => 'model_id',


    /*
     * The name of the column which holds the Class Name of the model related to the extras.
     *
     * You can change this value if you have set a different name in the migration for the extras table.
     */
    'model_name_attribute' => 'model_class',
];

使用方法

HasExtraFields 特性添加到您想要使用额外字段的模型中。

use Fahedaljghine\ExtraField\HasExtraFields;

class YourEloquentModel extends Model
{
    use HasExtraFields;
}

添加新的额外字段和值

您可以像这样添加一个新的额外字段

$extra_field = $model->addExtraField('fieldName' , 'fieldType');

//assign value
$extra_field = $model->addExtraValue($extra_field->id , 'filedValue');

//assign value for other instance no need to add extra field again
$extra_field = $otherModel->addExtraValue($extra_field->id , 'filedValue');

//for another instance
$extra_field = $anotherModel->addExtraValue($extra_field->id , 'filedValue');

添加新的字符串类型额外字段并分配值

如果您想为模型添加一个新的字符串类型额外字段,可以这样做

$model->addStringExtraValue('fieldName' , 'filedValue');

//you can repeat for other object
$otherModel->addExtraValue('fieldName', 'filedValue');

检索数据

// will give array of all extra fields with associated values
$model->getExtras(); // ['fieldName1' => 'filedValue' , 'fieldName2' => 'filedValue']


$model->extras(); // will return a collection of Fahedaljghine\ExtraField\Extra


$model->extraValues ; //will return a collection of Fahedaljghine\ExtraField\ExtraValue


$model->extraValues() ; // will return hasMany Relation of Fahedaljghine\ExtraField\ExtraValue

删除额外字段

//this wil drop the value of the given name extra field for this model
$model->dropExtraFieldData($name) ;

删除额外字段及其与模型的所有相关数据

请谨慎操作

//this will drop the extra data field and all associated data with it
$model->dropExtraField($name);

更新额外字段值

有时您可能需要更新额外字段的值,您可以通过使用以下方法实现

#$extra_field @param String the name of the extra field
#$updated_value @param String the new value

$model->updateExtraValue($extra_field , $updated_value);

自定义模型和迁移

您可以通过在 extra-field 配置文件中的 extra_model & extra_value_model 键中指定类名来更改使用的模型。

您可以在使用自定义迁移时更改 extra_values 表中使用的列名(默认为 model_id)。在这种情况下,只需更改 extra-field 配置文件中的 model_primary_key_attribute 键即可。

您可以在使用自定义迁移时更改 extra_values 表中使用的列名(默认为 model_class)。在这种情况下,只需更改 extra-field 配置文件中的 model_name_attribute 键即可。

更新日志

请参阅更新日志以获取有关最近更改的更多信息。

贡献

欢迎您贡献力量

Dontae

致谢

许可协议

MIT 许可协议 (MIT)。请参阅许可文件以获取更多信息。