pion/laravel-support-eloquent

提供Laravel Eloquent模型的小型支持特性和类。

v1.5.0-rc 2023-03-14 15:14 UTC

This package is auto-updated.

Last update: 2024-09-14 18:28:58 UTC


README

包含一组用于Eloquent模型的特性和类。未来可能包含更多用于Eloquent数据库的类/特性集。

安装

composer require pion/laravel-support-eloquent

属性值修改特性

将改变属性值的特性集。

CleanHTMLFromAttributeTrait

启用自动清理所有属性的HTML值,通过仅允许由$cleanAttributes属性指定的值或限制可以包含HTML的属性$dontCleanAttributes。您可以使用$stripHtmlTags属性指定不应去除的标签。

对于手动使用,请使用CleanHTMLTraittryToCleanAttributeValue($key, $value)方法。

仅给定属性为null

public $cleanAttributes = [
    "name"
];

不要将提供的属性设置为null

public $dontCleanAttributes = [
    "name"
];

NullEmptyStringAttributeTrait

启用自动将空字符串值(如从POST或SET获取的)设置为null。您可以提供列键的列表,允许仅指定的列(使用$nullEmptyAttributes)设置为null,或者提供列键的列表,在尝试将值设置为null时忽略(使用$dontNullEmptyAttributes)。它们可以在构造函数中设置或作为属性设置。

对于手动使用,请使用NullEmptyStringTraittryToNullAttributeValue($key, $value)方法。

仅给定属性为null

public $nullEmptyAttributes = [
    "name"
];

不要将提供的属性设置为null

public $dontNullEmptyAttributes = [
    "name"
];

NormalizeFloatAttributeTrait

通过$normalizeFloatAttributes设置属性列表,将浮点字符串转换为浮点值,支持逗号(floatval无法将13,3转换为13.3)。

public $normalizeFloatAttributes = [
    'price',
    'discount',
];

对于手动使用,请使用NormalizeFloatTraittryToNormalizeFloatAttributeValue($key, $value)方法。

DateAttributeValueTrait

将允许的属性(通过设置$dateAttributes)转换为没有格式限制的Carbon实例。尝试解析任何格式。

public $dateAttributes = ['custom_date'];

对于手动使用,请使用DateAttributeTraittryToConvertAttributeValueToDate($key, $value)方法。

所有值都通过toArray方法转换为默认格式。您可以通过设置dateFormats按属性自定义日期格式。

public $dateFormats = [
    'born' => 'Y-m-d'
];

运行多个特性函数

使用所有属性特性

要应用当前实现的所有特性,请使用AlterAttributeValueTrait

手册

不幸的是,特性无法覆盖相同的方法(在这种情况下为setAttribute)。为此,您必须自行重写setAttribute方法,并自行调用所需特性方法。

每个特性都有自己的手册方法,该方法尝试修改值。使用适当的特性(例如NullEmptyStringTraitCleanHTMLTrait等)。

要链接值,可以使用辅助函数alter_attribute_value

/**
 * Set a given attribute on the model.
 *
 * @param  string $key
 * @param  mixed  $value
 */
public function setAttribute($key, $value)
{
    parent::setAttribute($key, alter_attribute_value($key, $value, $this, [
        'tryToCleanAttributeValue',
        'tryToNullAttributeValue'
    ]));
}

关系特性

RelationJoinTrait

启用创建一个SQL连接语句,该语句将构造关系模型并将其存储在关系中(这样您就不需要懒加载关系)。模型是从关系函数(提供的键)创建的。您可以为自定义关系命名创建自定义别名。

默认情况下,将加载模式的所有列,为了更好的性能,您可以提供要从中加载关系的列的集合。您不能提供'*'作为列。

$relationAliases - 一个具有与表不同的方法名称的关系列表。

可以在模型中这样定义

protected $relationAliases = [
    "activity_type" => "type"
];

然后您可以通过标准方式调用它:modelJoin("type")用于ActivityType模型类。

示例

基本方法支持自定义列、条件、连接操作符和连接类型。

所有列
Model::modelJoin("type")->get()
所需列(推荐)
Model::modelJoin("type", ["name", "id", "color"])->get();

然后您可以通过标准关系方式获取对象

$model->type->color

但请注意,它可以返回null(默认为LEFT连接)!

需要与内连接的列
Model::modelJoin("type", ["name", "id", "color"], "inner")->get();
方法

文档在代码中提供。

modelJoin($query, $relation_name, $operatorOrColumns = '=', $type = 'left', $where = false, $columns = array())

高级示例

文档在代码中提供。使用表作为关系函数。

joinWithSelect($query, $table, $one, $operatorOrColumns, $two, $type = "left", $where = false, $columns = array())

RelationCountTrait

启用计数相关模型。未来将准备更好的文档。

示例

where的用法

$count = $model->relationCountWithWhere("user_permission", "user_id", $user, "App\\Models\\User");

再次调用该函数将使用关系数组中的缓存。在此调用之后,您还可以使用

$model->user_permission_{ForeignKey}_{userIdValueForWhere} which will the object of User model with count attribute.

通过传递变量,您可以获取where索引,该变量将被引用覆盖

$index = "user_permission";
$model->relationCountWithWhere($index, "user_id", $user, "App\\Models\\User");

简单调用将返回计数,索引将存储在 $model->user

$model->relationCount("user", "App\\Models\\User")