Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 17 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| TemplateCategory | |
0.00% |
0 / 17 |
|
0.00% |
0 / 3 |
30 | |
0.00% |
0 / 1 |
| boot | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
12 | |||
| templates | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| subcategory_lv1 | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Models; |
| 4 | |
| 5 | use Illuminate\Database\Eloquent\Relations\HasMany; |
| 6 | use Illuminate\Support\Facades\Validator; |
| 7 | use Illuminate\Validation\Rule; |
| 8 | |
| 9 | class TemplateCategory extends Moloquent |
| 10 | { |
| 11 | protected $table = 'template_categories'; |
| 12 | |
| 13 | protected $fillable = [ |
| 14 | 'name', |
| 15 | 'slug', |
| 16 | 'position' |
| 17 | ]; |
| 18 | |
| 19 | public static function boot(): void |
| 20 | { |
| 21 | parent::boot(); |
| 22 | |
| 23 | //Auto increment position field |
| 24 | static::creating(function ($model) { |
| 25 | if (!$model->position) { |
| 26 | $lastPosition = self::max('position') ?? 0; |
| 27 | $model->position = $lastPosition + 1; |
| 28 | } |
| 29 | }); |
| 30 | |
| 31 | // Validate uniqueness of position field |
| 32 | static::saving(function ($model) { |
| 33 | $validator = Validator::make([$model->getAttributes()], [ |
| 34 | 'position' => [ |
| 35 | Rule::unique('template_categories')->ignore($model->getKey()) |
| 36 | ], |
| 37 | ]); |
| 38 | |
| 39 | if ($validator->fails()) { |
| 40 | throw new \Exception($validator->errors()->first()); |
| 41 | } |
| 42 | }); |
| 43 | } |
| 44 | |
| 45 | public function templates(): HasMany |
| 46 | { |
| 47 | return $this->hasMany(Template::class, 'category_id'); |
| 48 | } |
| 49 | |
| 50 | public function subcategory_lv1(): HasMany |
| 51 | { |
| 52 | return $this->hasMany(TemplateSubcategoryLv1::class, 'parent_category_id'); |
| 53 | } |
| 54 | } |