pion/laravel-support-eloquent
Composer 安装命令:
composer require pion/laravel-support-eloquent
包简介
Package with small support traits and classes for the Laravel Eloquent models
README 文档
README
Contains a set of traits for the eloquent model. In future can contain more set of classes/traits for the eloquent database.
Installation
composer require pion/laravel-support-eloquent
Attribute value altering traits
Set of traits that will change the attribute value.
CleanHTMLFromAttributeTrait
Enables automatic attribute value cleaning from HTML for all attributes, by limiting only desired by $cleanAttributes
property or by limiting which attributes can have html $dontCleanAttributes. You can use the $stripHtmlTags property to
specify tags which should not be stripped.
For manual usage use CleanHTMLTrait with tryToCleanAttributeValue($key, $value) method.
Null only given attributes:
public $cleanAttributes = [ "name" ];
Don't null provided attributes:
public $dontCleanAttributes = [ "name" ];
NullEmptyStringAttributeTrait
Enables the automatic nulling of empty string value (like from post or set). You can provide
list of columns keys to allow only specified columns (use $nullEmptyAttributes) to be set to null or you can provide a
list of columns keys to ignore while trying to null the value (use $dontNullEmptyAttributes). They can be set in construct
or as property.
For manual usage use NullEmptyStringTrait with tryToNullAttributeValue($key, $value) method.
Null only given attributes:
public $nullEmptyAttributes = [ "name" ];
Don't null provided attributes:
public $dontNullEmptyAttributes = [ "name" ];
NormalizeFloatAttributeTrait
Converts float string to float value with comma support (floatval fails to convert 13,3 to 13.3) by setting list of attributes via $normalizeFloatAttributes.
public $normalizeFloatAttributes = [
'price',
'discount',
];
For manual usage use NormalizeFloatTrait with tryToNormalizeFloatAttributeValue($key, $value) method.
DateAttributeValueTrait
Converts allowed attributes (by settings $dateAttributes) to carbon instance without any format limitation. Tries to parse any format.
public $dateAttributes = ['custom_date'];
For manual usage use DateAttributeTrait with tryToConvertAttributeValueToDate($key, $value) method.
All values are converted via toArray method to default format. You can customize date formats by attribute by setting dateFormats:
public $dateFormats = [ 'born' => 'Y-m-d' ];
Running multiple trait functions
Using all attributes traits
To apply all traits that are currently implemented use AlterAttributeValueTrait.
Manual
Unfortunately traits can't override same method (in this case setAttribute). For this purpose, you must override the setAttribute
method by your self and call the desired trait method by your self.
Every trait has own manual method that tries to alter the value. Use appropriate trait (NullEmptyStringTrait, CleanHTMLTrait, etc).
For chaining the value you can use helper function 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' ])); }
Relation Traits
RelationJoinTrait
Enables to create a join SQL statement that will construct the relation model and stores it into relations (so you don't need to eager load the relation). The model is created from the relation function (the key you provide). You can create a custom aliases to fix custom relation naming.
In default usage will load all columns from schema, for better perfomance you can provide a set of columns to load from the relation. You can't provide a '*' as column.
$relationAliases - A list of relations that has different method name than the table.
Can be defined in model like this:
protected $relationAliases = [ "activity_type" => "type" ];
Then you can call it in standard way modelJoin("type") for a ActivityType model class.
Example
The basic method support custom columns, where condition, join operator and join type.
All columns
Model::modelJoin("type")->get()
Desired columns (recommended)
Model::modelJoin("type", ["name", "id", "color"])->get();
Then you can get the object by standard relation way:
$model->type->color
But be carefull, can be null (default is LEFT connection)!
Desired columns with inner join
Model::modelJoin("type", ["name", "id", "color"], "inner")->get();
Method
Docs is provided in code.
modelJoin($query, $relation_name, $operatorOrColumns = '=', $type = 'left', $where = false, $columns = array())
Advanced example
Docs is provided in code. Uses table as a relation function.
joinWithSelect($query, $table, $one, $operatorOrColumns, $two, $type = "left", $where = false, $columns = array())
RelationCountTrait
Enables to count a related models. In future will prepare better docs.
Example
Usage of where:
$count = $model->relationCountWithWhere("user_permission", "user_id", $user, "App\\Models\\User");
Calling the function again will use the cache in relations array. After this call you can also use
$model->user_permission_{ForeignKey}_{userIdValueForWhere} which will the object of User model with count attribute.
You can also get the where index by passing variable which will be overided by the reference:
$index = "user_permission"; $model->relationCountWithWhere($index, "user_id", $user, "App\\Models\\User");
Simple call will return count and the index will be stored in $model->user
$model->relationCount("user", "App\\Models\\User")
Testing
Run the full support matrix and get a pass/fail table:
bin/test-matrix.sh
Run the PHPUnit suite in Docker for each supported PHP runtime:
docker compose run --rm php82 composer test docker compose run --rm php83 composer test docker compose run --rm php85 composer test
Resolve and test a specific Laravel database line inside Docker:
docker compose run --rm php82 bin/test-target.sh '^9.0' docker compose run --rm php82 bin/test-target.sh '^10.0' docker compose run --rm php83 bin/test-target.sh '^11.0' docker compose run --rm php83 bin/test-target.sh '^12.0' docker compose run --rm php85 bin/test-target.sh '^13.0'
pion/laravel-support-eloquent 适用场景与选型建议
pion/laravel-support-eloquent 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.92k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2016 年 05 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 pion/laravel-support-eloquent 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 pion/laravel-support-eloquent 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 6.92k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-05-04