ldc-legaulois/app/Models/Message.php

103 lines
2.4 KiB
PHP
Raw Normal View History

2024-07-12 16:33:26 +02:00
<?php
namespace App\Models;
use App\Casts\NiceDate;
2024-07-30 16:05:27 +02:00
use App\Models\Notification;
2024-07-12 16:33:26 +02:00
use App\Interfaces\Notifiable as INotifiable;
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;
use Illuminate\Database\Eloquent\Builder;
2024-07-12 16:33:26 +02:00
class Message extends Model implements INotifiable
{
/**
* The attributes that are mass assignable.
*
* @var array<integer,string>
*/
protected $fillable = [
'message',
'to'
];
2024-07-17 16:26:57 +02:00
/**
* The attributes that should be cast.
*
* @var array<string,string>
*/
protected $casts = [
'message' => 'encrypted'
];
2024-07-12 16:33:26 +02:00
// * RELATIONSHIPS
/**
* BelongsTo customer relation.
2024-07-12 16:33:26 +02:00
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2024-07-30 16:05:27 +02:00
public function toCustomer(): \Illuminate\Database\Eloquent\Relations\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
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2024-07-30 16:05:27 +02:00
public function fromCustomer(): \Illuminate\Database\Eloquent\Relations\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
*
* @return \Illuminate\Database\Eloquent\Relations\MorphOne
*/
2024-07-30 16:05:27 +02:00
public function notifiable(): \Illuminate\Database\Eloquent\Relations\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
*
* @return \Illuminate\Database\Eloquent\Casts\Attribute
*/
2024-07-30 16:05:27 +02:00
public function createdAtNiceDate(): \Illuminate\Database\Eloquent\Casts\Attribute
2024-07-16 10:47:23 +02:00
{
return Attribute::make(
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
*
* @param integer $days
* @return Builder
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
*
* @return array
*/
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(), [
'created_at' => $this->created_at
]);
}
}