ldc-legaulois/app/Models/Message.php

91 lines
1.9 KiB
PHP
Raw Permalink Normal View History

2024-07-12 16:33:26 +02:00
<?php
namespace App\Models;
use App\Casts\NiceDate;
use App\Interfaces\Notifiable as INotifiable;
2026-09-25 16:25:31 +02:00
use Illuminate\Database\Eloquent\Builder;
2024-07-16 10:47:23 +02:00
use Illuminate\Database\Eloquent\Casts\Attribute;
2024-07-12 16:33:26 +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\MorphOne;
2024-07-12 16:33:26 +02:00
class Message extends Model implements INotifiable
{
/**
* The attributes that are mass assignable.
*
2026-09-25 16:25:31 +02:00
* @var array<int,string>
2024-07-12 16:33:26 +02:00
*/
protected $fillable = [
'message',
2026-09-25 16:25:31 +02:00
'to',
2024-07-12 16:33:26 +02:00
];
2024-07-17 16:26:57 +02:00
/**
* The attributes that should be cast.
*
* @var array<string,string>
*/
protected $casts = [
2026-09-25 16:25:31 +02:00
'message' => 'encrypted',
2024-07-17 16:26:57 +02:00
];
2024-07-12 16:33:26 +02:00
// * RELATIONSHIPS
/**
* BelongsTo customer relation.
2024-07-12 16:33:26 +02:00
*/
2026-09-25 16:25:31 +02:00
public function toCustomer(): BelongsTo
2024-07-12 16:33:26 +02:00
{
return $this->belongsTo(Customer::class, 'to');
}
/**
* BelongsTo customer relation.
2024-07-12 16:33:26 +02:00
*/
2026-09-25 16:25:31 +02:00
public function fromCustomer(): BelongsTo
2024-07-12 16:33:26 +02:00
{
return $this->belongsTo(Customer::class, 'from');
}
/**
* MorphOne notification relation.
2024-07-12 16:33:26 +02:00
*/
2026-09-25 16:25:31 +02:00
public function notifiable(): MorphOne
2024-07-12 16:33:26 +02:00
{
2024-07-30 16:05:27 +02:00
return $this->morphOne(Notification::class, 'notifiable');
2024-07-12 16:33:26 +02:00
}
// * METHODS
2024-07-16 10:47:23 +02:00
/**
* Created at nice date.
2024-07-16 10:47:23 +02:00
*/
2026-09-25 16:25:31 +02:00
public function createdAtNiceDate(): Attribute
2024-07-16 10:47:23 +02:00
{
return Attribute::make(
2026-09-25 16:25:31 +02:00
get: fn () => (new NiceDate())->get(null, '', $this->created_at, [])
2024-07-16 10:47:23 +02:00
);
}
2024-07-12 16:33:26 +02:00
/**
* Get older messages.
2024-07-12 16:33:26 +02:00
*/
public static function getMessagesOlderThan(int $days): Builder
2024-07-12 16:33:26 +02:00
{
return self::query()->where('updated_at', '<', now()->subDays($days));
}
/**
* Convert the model instance to an array.
* ! Make sure we use correct nicedate formater
*/
2024-07-30 16:05:27 +02:00
public function toArray(): array
2024-07-12 16:33:26 +02:00
{
return array_merge(parent::toArray(), [
2026-09-25 16:25:31 +02:00
'created_at' => $this->created_at,
2024-07-12 16:33:26 +02:00
]);
}
}