ldc-boutique-cnc/app/Models/Contact.php
2023-02-13 14:57:01 +01:00

88 lines
2 KiB
PHP

<?php
namespace App\Models;
use App\Notifications\ContactForm as NotificationContactForm;
use App\Lib\Helpers\FileStorageHelper;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Notification;
use Kwaadpepper\Enum\Traits\CastsEnums;
class Contact extends Model
{
use HasFactory;
use CastsEnums;
use Notifiable;
/**
* The attributes that should be cast.
*
* @var array
*/
public $casts = [
'done' => 'bool'
];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'firstname',
'lastname',
'subject',
'email',
'message',
'order_number',
'file',
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
// * EVENT CLOSURES.
static::creating(function (self $model) {
// * Store joined file.
if ($model->file) {
$model->file = FileStorageHelper::storeFile($model, $model->file, true);
}
static::sendEmailContact($model);
});
static::deleted(function (self $model) {
if ($model->file) {
FileStorageHelper::removeFile($model, 'file');
}
});
}
/**
* Send the email contact.
*
* @param \App\Models\Contact $model
* @return void
*/
public function sendEmailContact(Contact $model)
{
Notification::route('mail', config('mail.contactsList.serviceweb'))
->notify(new NotificationContactForm($model));
}
// * RELATIONSHIPS
/**
* BelongsTo order relation
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function order()
{
return $this->belongsTo(Order::class, 'order_number', 'reference');
}
}