ldc-legaulois/app/Models/Message.php

88 lines
1.8 KiB
PHP
Raw Permalink Normal View History

2021-03-10 17:16:31 +01:00
<?php
namespace App\Models;
use App\Casts\NiceDate;
use Illuminate\Database\Eloquent\Model;
2021-03-11 08:22:31 +01:00
use Illuminate\Support\Carbon;
2021-03-10 17:16:31 +01:00
class Message extends Model
{
/**
* The attributes that should be cast.
*
* @var array
*/
2021-03-10 17:16:31 +01:00
protected $casts = [
'created_at' => NiceDate::class,
];
2021-03-20 14:38:15 +01:00
/**
* The attributes that are mass assignable.
*
* @var array
*/
2021-03-10 17:16:31 +01:00
protected $fillable = [
'message',
'to'
];
// * RELATIONSHIPS
/**
* BelongsTo customer relation
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2021-03-10 17:16:31 +01:00
public function toCustomer()
{
return $this->belongsTo(Customers::class, 'to');
}
/**
* BelongsTo customer relation
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2021-03-10 17:16:31 +01:00
public function fromCustomer()
{
return $this->belongsTo(Customers::class, 'from');
}
2021-03-11 08:22:31 +01:00
/**
* 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
*/
2021-03-11 08:22:31 +01:00
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
]);
}
2021-03-10 17:16:31 +01:00
}