95 lines
2.6 KiB
PHP
95 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Casts\NiceDate;
|
|
use App\Enums\NotificationType;
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\View;
|
|
|
|
/**
|
|
* @property \App\Enums\NotificationType $type Notification type.
|
|
* @property integer $notifiable_id Notification slug.
|
|
* @property integer|null $customer_id Notification customer id.
|
|
* @property string $notifiable_type Notification slug.
|
|
* @property string $content Notification content.
|
|
* @property-read \Carbon\Carbon $created_at Created date.
|
|
* @property-read \Carbon\Carbon $updated_at Updated date.
|
|
*/
|
|
class Notification extends Model
|
|
{
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<integer, string>
|
|
*/
|
|
protected $fillable = [
|
|
'type',
|
|
'content',
|
|
'customer_id'
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string,string>
|
|
*/
|
|
protected $casts = [
|
|
'type' => NotificationType::class
|
|
];
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Created at nice date.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Casts\Attribute
|
|
*/
|
|
public function createdAtNiceDate(): \Illuminate\Database\Eloquent\Casts\Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn() => (new NiceDate())->get(null, '', $this->created_at, [])
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get notifications.
|
|
*
|
|
* @return void
|
|
*/
|
|
public static function getNotifications(): void
|
|
{
|
|
if (!Auth::guard('club')->check()) {
|
|
return;
|
|
}
|
|
/** @var \App\Models\Customer $customerModel */
|
|
$customerModel = Auth::guard('club')->user();
|
|
$notifications = $customerModel->getAllNotifications();
|
|
View::share('notifications', count($notifications) - $customerModel->readNotifications()->count());
|
|
View::share('notificationsList', $notifications);
|
|
}
|
|
|
|
// * RELATIONSHIPS
|
|
|
|
/**
|
|
* Get the customer that owns the Notifications.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function customer(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
{
|
|
return $this->belongsTo(Customer::class);
|
|
}
|
|
|
|
/**
|
|
* Polymorphic relation : Get the parent model.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
|
|
*/
|
|
public function notifiable(): \Illuminate\Database\Eloquent\Relations\MorphTo
|
|
{
|
|
return $this->morphTo();
|
|
}
|
|
}
|