*/ protected $fillable = [ 'reference', 'first_name', 'last_name', 'email', 'address', 'city', 'postcode', 'file', 'message', 'response_type', 'status', 'type', 'brand_id', 'subject_id', 'customer_id', ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'response_type' => ClaimResponseType::class, 'status' => ClaimStatus::class, 'type' => ClaimType::class, ]; /** * Perform any actions required after the model boots. * * @return void */ protected static function booted(): void { static::created(function (self $model) { $model->reference = self::encodeIntegerToBase36($model->getKey()); $model->saveOrFail(); }); static::updated(function (self $model) { $user = auth()->user(); if (!$user instanceof \App\Models\User) { $user = null; } if (isset($model->getChanges()['brand_id'])) { event(new ClaimModelChanged( $model->load('brand'), 'brand', Str::singular(trans('models.classes.brands')), $user, )); } if (isset($model->getChanges()['subject_id'])) { event(new ClaimModelChanged( $model->load('claimSubject'), 'claimSubject', trans('models.classes.claimsubjects'), $user, )); } if (isset($model->getChanges()['status'])) { event(new ClaimModelChanged( $model, 'status', trans('validation.custom.status'), $user, )); } if (isset($model->getChanges()['type'])) { event(new ClaimModelChanged( $model, 'type', trans('validation.custom.type'), $user, )); } if ( isset($model->getChanges()['response_type']) && !$model->response_type->equals(ClaimResponseType::manual) ) { event(new ClaimModelChanged( $model, 'response_type', trans('validation.custom.response_type'), $user, )); } }); } // * RELATIONS /** * Brand belongs to relation. * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function brand(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(Brand::class); } /** * ClaimSubject subject belongs to relation. * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function claimSubject(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(ClaimSubject::class, 'subject_id'); } /** * Customer subject belongs to relation. * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function customer(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(Customer::class); } /** * ClaimMessage has many relation. * * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function claimMessages(): \Illuminate\Database\Eloquent\Relations\HasMany { return $this->hasMany(ClaimMessage::class); } // * METHODS /** * Convert the integer to base 36 and make uppercase. * * @param integer $int * @return string */ public static function encodeIntegerToBase36(int $int): string { return strtoupper(base_convert($int, 10, 36)); } /** * Convert back to base 10 from base 36, case insensitive. * * @param string $str * @return string */ public static function decodeBase36ToInteger(string $str): string { return base_convert(strtolower($str), 36, 10); } /** * Get formated address. * * @return string */ public function getFormatedAddress(): string { return implode(', ', array_filter([ $this->address, trim(implode(' ', array_filter([$this->postcode, $this->city]))) ])); } }