lookfeel / laravel-eloquent-append-automate
根据 appends 配置的关联字段映射关系,自动维护需要的 appends。
Installs: 5 705
Dependents: 1
Suggesters: 0
Security: 0
Stars: 5
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=5.6.4
- illuminate/cache: >=6.0
- illuminate/console: >=6.0
- illuminate/database: >=6.0
- illuminate/support: >=6.0
Requires (Dev)
- fzaninotto/faker: ~1.8
- laravel/laravel: >=6.0
- phpunit/phpunit: ~5.0
This package is not auto-updated.
Last update: 2024-11-16 02:21:41 UTC
README
撸了个包,解决困扰我很久的一个问题,当给定了 $appends 之后,在某些限定了查询字段的查询时,会因没查询 accessor 中涉及的字段而导致错误,而每次在使用的时候再去动态 append 又很烦,变化时维护起来很心累。
根据 appends 配置的关联字段映射关系,自动维护需要的 appends。
安装
$ composer require lookfeel/laravel-eloquent-append-automate
Note: Laravel Eloquent Append Automation requires Laravel 5.6+.
使用
Appends 字段映射配置
如下所见,您仅仅需要给需要做字段映射的 append 添加对应的键名,键名为字映射字段名称,程序将自动根据映射关系,判断是否需要移除这些 append,以避免因查询中缺少该字段而引起的错误。
Model:
use Lookfeel\AppendAutomate\Database\Eloquent\Model; class User extend Modal { protected $appends = [ 'first_letter', 'firstname|lastname' => 'fullname', // firstname 和 lastname 字段缺一不可,否则不返回 fullname 'gender' => 'gender_text', // gender 是一个 int 字段,0:女,1:男 'status' => 'status_text', // status 是一个 int 字段,0:禁用,1:启用 'gender_text' => 'access', 'access' => 'access_text', ]; public function getFirstLetterAttribute() { return substr($this->firstname, 0, 1); } public function getFullnameAttribute() { return $this->firstname . ' ' . $this->lastname; } public function getGenderTextAttribute() { return ['女', '男'][$this->gender]; } public function getStatusTextAttribute() { return ['启用', '禁用'][$this->status]; } public function getAccessAttribute() { return $this->gender_text === 'Female'; } public function getAccessTextAttribute() { return $this->access ? 'can' : 'Can not'; } }
Controller
User::select(['id', 'firstname', 'gender'])->firstOrFail(); /*** { "id": 1, "first_letter": "T", "firstname": "Terran", "gender": 1, "gender_text": "男", "access": 1, "access_text": "can" } */
使用 Lookfeel\AppendAutomate\Database\Eloquent\Model
替换 Illuminate\Database\Eloquent\Model
只需将 Model 继承 Lookfeel\AppendAutomate\Database\Eloquent\Model
即可. Lookfeel\AppendAutomate\Database\Eloquent\Model
继承自 Eloquent
。
使用 Lookfeel\AppendAutomate\AppendAutomateTrait
如果由于某些原因不能继承 Lookfeel\AppendAutomation\Database\Model
,那么您可以在已有 Model 中 use Lookfeel\AppendAutomation\AppendAutomateTrait
。
相关文章:
https://learnku.com/articles/31931
单元测试
$ composer install --dev
$ vendor/bin/phpunit
License
**Laravel Eloquent Append Automation ** is licensed under the MIT License.