yuptogun / laravel-model-traits
utility traits and interfaces that have helped myself
dev-main
2021-03-30 06:52 UTC
Requires
- illuminate/database: ^8.34
This package is auto-updated.
Last update: 2024-10-29 05:54:08 UTC
README
I've helped myself with these code snippets. Hope someone else would be helped too.
Quick start: examples
Activation
class Membership extends \Illuminate\Database\Eloquent\Model, \Yuptogun\LaravelModel\Interfaces\ActivatableModel { use \Yuptogun\LaravelModel\Traits\HasActivationAttributes; public function getStartsAtFieldAttribute() { return 'membership_begins_at'; } public function getEndsAtFieldAttribute() { return 'membership_expires_at'; } } if (!$user->membership->is_active) { throw new \Exception('please get a membership!'); }
Rotating values
class User extends \Illuminate\Database\Eloquent\Model, \Yuptogun\LaravelModel\Interfaces\ValueRotatingModel { use \Yuptogun\LaravelModel\Traits\HasRotatingAttributes; public function getRotatingFieldsAttribute() { return [ 'hidden' => [1, 0], ]; } } if ($user->switchField('hidden')) { return 'switched visibility!'; }
Sortable
class Episode extends \Illuminate\Database\Eloquent\Model, \Yuptogun\LaravelModel\Interfaces\SortableModel { use \Yuptogun\LaravelModel\Traits\HasSortableOrderAttribute; public function getSortableOrderFieldAttribute() { return 'episode_sort'; } } $episode_sort = request()->input('episode'); if ((new Episode)->sortBySortableOrder($episode_sort)) { return 'reordered!'; }
Recursive models
class Curriculum extends \Illuminate\Database\Eloquent\Model, \Yuptogun\LaravelModel\Interfaces\RecursiveModel { use \Yuptogun\LaravelModel\Traits\IsRecursiveModel; public function getCteKeyChildAttribute() { return 'crcl_no'; } public function getCteKeyParentAttribute() { return 'p_crcl_no'; } } if ($curri->allParents()->grandParents()->first()->crcl_no == 2) { return 'Second grade curriculum'; }