ldc-legaulois/app/Models/Message.php
Jérémy Munsch 42eddd0040
Ticket 682 :
- Add Vue debug mode
- Show club.messages in reverse order + fix nice date formatter to using server timezone when json cast + fix get last message date for left column
2022-03-28 15:55:44 +02:00

87 lines
1.8 KiB
PHP

<?php
namespace App\Models;
use App\Casts\NiceDate;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
class Message extends Model
{
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'created_at' => NiceDate::class,
];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'message',
'to'
];
// * RELATIONSHIPS
/**
* BelongsTo customer relation
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function toCustomer()
{
return $this->belongsTo(Customers::class, 'to');
}
/**
* BelongsTo customer relation
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function fromCustomer()
{
return $this->belongsTo(Customers::class, 'from');
}
/**
* MorphOne notification relation
*
* @return \Illuminate\Database\Eloquent\Relations\MorphOne
*/
public function notifiable()
{
return $this->morphOne(Notifications::class, 'notifiable');
}
// * METHODS
/**
* Get MessagesOlderThan attribute
*
* @param integer $days
* @return \Illuminate\Database\Query\Builder
*/
public static function getMessagesOlderThan(int $days)
{
return Message::where('updated_at', '<', Carbon::now()->subDays($days));
}
/**
* Convert the model instance to an array.
* ! Make sure we use correct nicedate formater
*
* @return array
*/
public function toArray()
{
return array_merge(parent::toArray(), [
'created_at' => $this->created_at
]);
}
}