jgrossi/laravel-mutable
Composer 安装命令:
composer require jgrossi/laravel-mutable
包简介
Change model attributes values easily
README 文档
README
Change Laravel models
toArray()values using a simple and clean way
Installation
composer require jgrossi/laravel-mutable
Usage
First add Mutable trait to the model you want to change values:
use Jgrossi\Mutable\Mutable; class User extends Eloquent { use Mutable; }
Then create a UserMutator class in any place in your app (or give it other name if you prefer). Then set the $mutator property in your model:
use App\Models\Mutators\UserMutator; use Jgrossi\Mutable\Mutable; class User extends Eloquent { use Mutable; protected $mutator = UserMutator::class; }
In your mutator class you might have one method for each attribute you want to change:
namespace App\Models\Mutators; use Carbon\Carbon; use Jgrossi\Mutable\Mutator; class UserMutator extends Mutator { public function firstName($value) { return ucfirst($value); } public function createdAt(Carbon $date) { return $date->format('Y-m-d'); } }
Then when using $user->toArray() you'll have the first_name attributes changed.
class FooController extends Controller { public function show($id) { $user = User::findOrFail($id); return $user; // returns the changed User as array } }
Licence
MIT License © Junior Grossi
统计信息
- 总下载量: 23
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-07-20