Edit File: Contact.php
<?php namespace App\Models; use App\Mail\SendMail; 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 = ['name', 'phone', 'email', 'title', 'message', 'image', 'type', 'country_code', 'contactable_id', 'contactable_type']; /** * @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 setEmailAttribute($value): void { // check if the contact has contactable $this->attributes['email'] = $this->contactable ? $this->contactable->email : $value; } public function setPhoneAttribute($value): void { // check if the contact has contactable $this->attributes['phone'] = $this->contactable ? $this->contactable->phone : $value; } public function setNameAttribute($value): void { // check if the contact has contactable $this->attributes['name'] = $this->contactable ? $this->contactable->name : $value; } /** * 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()); } } }
Back to File Manager