Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
100.00% |
1 / 1 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
26 / 26 |
| Question | |
100.00% |
1 / 1 |
|
100.00% |
3 / 3 |
3 | |
100.00% |
26 / 26 |
| __construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
24 / 24 |
|||
| brand | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| answers | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Models; |
| 4 | |
| 5 | use App\Enums\QuestionType; |
| 6 | use Illuminate\Database\Eloquent\Factories\HasFactory; |
| 7 | use Illuminate\Database\Eloquent\Model; |
| 8 | use Illuminate\Support\Str; |
| 9 | use Kwaadpepper\CrudPolicies\Enums\CrudAction; |
| 10 | use Kwaadpepper\CrudPolicies\Enums\CrudType; |
| 11 | use Kwaadpepper\CrudPolicies\Traits\IsCrudModel; |
| 12 | use Kwaadpepper\Enum\Traits\CastsEnums; |
| 13 | |
| 14 | class Question extends Model |
| 15 | { |
| 16 | use CastsEnums; |
| 17 | use HasFactory; |
| 18 | use IsCrudModel; |
| 19 | |
| 20 | /** @var string */ |
| 21 | protected $crudLabelColumn = 'question'; |
| 22 | |
| 23 | /** @var string */ |
| 24 | protected $crudValueColumn = 'id'; |
| 25 | |
| 26 | /** |
| 27 | * Map attribute names to enum classes. |
| 28 | * |
| 29 | * @var array |
| 30 | */ |
| 31 | protected $enumCasts = [ |
| 32 | 'type' => QuestionType::class |
| 33 | ]; |
| 34 | |
| 35 | /** |
| 36 | * The attributes that are mass assignable. |
| 37 | * |
| 38 | * @var string[] |
| 39 | */ |
| 40 | protected $fillable = [ |
| 41 | 'question', |
| 42 | 'type' |
| 43 | ]; |
| 44 | |
| 45 | /** |
| 46 | * Question |
| 47 | * |
| 48 | * @param array $attributes |
| 49 | */ |
| 50 | public function __construct(array $attributes = []) |
| 51 | { |
| 52 | self::$editableProperties = [ |
| 53 | 'question' => [ |
| 54 | 'label' => Str::ucfirst(trans('models.common.question')), |
| 55 | 'type' => CrudType::string(), |
| 56 | 'validate' => [ |
| 57 | 'min:10', |
| 58 | 'max:255' |
| 59 | ], |
| 60 | 'actions' => [ |
| 61 | CrudAction::viewAny(), |
| 62 | CrudAction::view(), |
| 63 | CrudAction::create(), |
| 64 | CrudAction::update() |
| 65 | ] |
| 66 | ], |
| 67 | 'type' => [ |
| 68 | 'label' => Str::ucfirst(trans('models.common.type')), |
| 69 | 'type' => CrudType::enum(), |
| 70 | 'enum' => QuestionType::class, |
| 71 | 'actions' => [ |
| 72 | CrudAction::view(), |
| 73 | CrudAction::create(), |
| 74 | CrudAction::update(), |
| 75 | ] |
| 76 | ], |
| 77 | 'brand_id' => [ |
| 78 | 'label' => Str::ucfirst(trans('models.classes.brand')), |
| 79 | 'type' => CrudType::belongsTo(), |
| 80 | 'belongsTo' => Brand::class, |
| 81 | 'actions' => [ |
| 82 | CrudAction::viewAny(), |
| 83 | CrudAction::view(), |
| 84 | CrudAction::create(), |
| 85 | CrudAction::update(), |
| 86 | ] |
| 87 | ], |
| 88 | 'answers' => [ |
| 89 | 'label' => Str::ucfirst(Str::plural(trans('models.classes.answers'))), |
| 90 | 'type' => CrudType::belongsToMany(), |
| 91 | 'belongsToMany' => Answer::class, |
| 92 | 'belongsToManyLabel' => 'answer', |
| 93 | 'actions' => [ |
| 94 | CrudAction::view() |
| 95 | ] |
| 96 | ] |
| 97 | ]; |
| 98 | parent::__construct($attributes); |
| 99 | } |
| 100 | |
| 101 | // * RELATIONS |
| 102 | |
| 103 | /** |
| 104 | * Brand BelongsTo relation |
| 105 | * |
| 106 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
| 107 | */ |
| 108 | public function brand() |
| 109 | { |
| 110 | return $this->belongsTo(Brand::class); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Answer BelongsTo relation |
| 115 | * |
| 116 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
| 117 | */ |
| 118 | public function answers() |
| 119 | { |
| 120 | return $this->hasMany(Answer::class); |
| 121 | } |
| 122 | } |