Edit File: Contact.php
<?php namespace App\Models; use App\Enums\ContactType; use App\Jobs\SendAdminNotification; use App\Mail\SendMail; use App\Notifications\ContactNotification; use App\Traits\ModelTrait; use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Database\Eloquent\Relations\MorphTo; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Mail; class Contact extends BaseModel { use ModelTrait; protected const IMAGEPATH = 'contacts'; protected $fillable = [ 'contactable_id', 'contactable_type', 'name', 'phone', 'email', 'title', 'message', 'image', 'type', 'country_code', ]; public $with = [ 'contactable', ]; /** * @return HasOne */ public function reply(): HasOne { return $this->hasOne(ContactReplay::class, 'contact_id', 'id'); } /** * @return MorphTo */ public function contactable(): MorphTo { return $this->morphTo(); } // mutators public function getNameAttribute() { return $this->contactable ? $this->contactable->full_name : $this->attributes['name']; } public function getPhoneAttribute() { return $this->contactable ? $this->contactable->phone : $this->attributes['phone']; ; } public function getCountryCodeAttribute() { return $this->contactable ? $this->contactable->country_code : $this->attributes['country_code']; } public function getFullPhoneAttribute() { return $this->contactable ? $this->contactable->full_phone : ($this->attributes['country_code'] . $this->attributes['phone']); } public function getEmailAttribute() { return $this->contactable ? $this->contactable->email : $this->attributes['email']; } public function getTypeTextAttribute() { return $this->type == ContactType::COMPLAINT ? __('admin.complaint') : __('admin.question'); } /** * Send replay email to the contact * @param $replay */ public function sendReplayEmail(string $replay): void { try { Mail::to($this->email)->send(new SendMail([ 'title' => 'Replay to your message', 'message' => $replay ])); } catch (\Exception $e) { // Log the exception or handle it as needed Log::error('Failed to send replay email: ' . $e->getMessage()); } } public static function boot() { parent::boot(); static::created(function ($model) { $contactable = $model->contactable; SendAdminNotification::dispatchAfterResponse( $model, ContactNotification::class, 'رساله جديده من قبل ' . ($contactable ? ($contactable->full_name ?? 'مستخدم') : 'زائر'), 'A New Contact Message From ' . ($contactable ? ($contactable->full_name ?? 'User') : 'Guest'), $model->message, $model->message, 'new_contact', ); }); } }
Back to File Manager