ldc-legaulois/app/Models/Notification.php

90 lines
2.3 KiB
PHP
Raw Permalink Normal View History

2024-07-11 15:46:43 +02:00
<?php
namespace App\Models;
2024-07-15 11:09:34 +02:00
use App\Casts\NiceDate;
2024-07-11 15:46:43 +02:00
use App\Enums\NotificationType;
2024-07-15 11:09:34 +02:00
use Illuminate\Database\Eloquent\Casts\Attribute;
2024-07-11 15:46:43 +02:00
use Illuminate\Database\Eloquent\Model;
2026-09-25 16:25:31 +02:00
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\View;
2024-07-11 15:46:43 +02:00
2024-07-30 16:05:27 +02:00
/**
2026-09-25 16:25:31 +02:00
* @property \App\Enums\NotificationType $type Notification type.
* @property int $notifiable_id Notification slug.
* @property int|null $customer_id Notification customer id.
* @property string $notifiable_type Notification slug.
* @property string $content Notification content.
2024-07-30 16:05:27 +02:00
* @property-read \Carbon\Carbon $created_at Created date.
* @property-read \Carbon\Carbon $updated_at Updated date.
*/
class Notification extends Model
2024-07-11 15:46:43 +02:00
{
/**
* The attributes that are mass assignable.
*
2026-09-25 16:25:31 +02:00
* @var array<int, string>
2024-07-11 15:46:43 +02:00
*/
protected $fillable = [
'type',
'content',
2026-09-25 16:25:31 +02:00
'customer_id',
2024-07-11 15:46:43 +02:00
];
2024-07-15 10:23:12 +02:00
2024-07-30 16:05:27 +02:00
/**
* The attributes that should be cast.
*
* @var array<string,string>
*/
protected $casts = [
2026-09-25 16:25:31 +02:00
'type' => NotificationType::class,
2024-07-30 16:05:27 +02:00
];
// * METHODS
2024-07-15 10:23:12 +02:00
/**
* Created at nice date.
2024-07-15 10:23:12 +02:00
*/
2026-09-25 16:25:31 +02:00
public function createdAtNiceDate(): Attribute
2024-07-15 10:23:12 +02:00
{
return Attribute::make(
2026-09-25 16:25:31 +02:00
get: fn () => (new NiceDate())->get(null, '', $this->created_at, [])
);
2024-07-15 10:23:12 +02:00
}
/**
* Get notifications.
2024-07-15 10:23:12 +02:00
*/
public static function getNotifications(): void
2024-07-15 10:23:12 +02:00
{
2026-09-25 16:25:31 +02:00
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);
2024-07-15 10:23:12 +02:00
}
2024-07-15 11:09:34 +02:00
// * RELATIONSHIPS
2024-07-15 11:09:34 +02:00
/**
* Get the customer that owns the Notifications.
2024-07-15 11:09:34 +02:00
*/
2026-09-25 16:25:31 +02:00
public function customer(): BelongsTo
2024-07-15 11:09:34 +02:00
{
return $this->belongsTo(Customer::class);
}
/**
* Polymorphic relation : Get the parent model.
*/
2026-09-25 16:25:31 +02:00
public function notifiable(): MorphTo
{
return $this->morphTo();
2024-07-15 11:09:34 +02:00
}
2024-07-11 15:46:43 +02:00
}