This repository has been archived by the owner on Aug 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTicketMessage.php
156 lines (137 loc) · 4.06 KB
/
TicketMessage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
namespace SanjabTicket\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
use SanjabTicket\Observers\TicketMessageObserver;
/**
* @property int $ticket_id ticket of this message.
* @property int $user_id user who sent this message.
* @property string $text message text.
* @property null|string $file file path.
* @property null|int $seen_id who seen this message.
* @property null|\Illuminate\Support\Carbon $seen_at when this message seen.
* @property-read string $created_at_diff created_at in diff for humans format.
* @property-read string $updated_at_diff updated_at in diff for humans format.
* @property-read null|string $file_link full url of file.
*/
class TicketMessage extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'ticket_id',
'user_id',
'text',
'file',
'seen_at',
'seen_id',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'seen_at' => 'datetime',
];
/**
* The relationships that should be touched on save.
*
* @var array
*/
protected $touches = ['ticket'];
/**
* The relations to eager load on every query.
*
* @var array
*/
protected $with = ['user', 'seenBy'];
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = ['created_at_diff', 'updated_at_diff', 'file_link'];
/* -------------------------------- Relations ------------------------------- */
/**
* Ticket of this message.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function ticket()
{
return $this->belongsTo(Ticket::class);
}
/**
* User that sent this message.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
return $this->belongsTo(config('sanjab-ticket.database.model'), 'user_id');
}
/**
* User that seen this message.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function seenBy()
{
return $this->belongsTo(config('sanjab-ticket.database.model'), 'seen_id');
}
/* -------------------------------- Functions ------------------------------- */
/**
* The "booting" method of the model.
*
* @return void
*/
protected static function boot()
{
parent::boot();
static::observe(TicketMessageObserver::class);
}
/**
* Get the table associated with the model.
*
* @return string
*/
public function getTable()
{
return config('sanjab-ticket.tables.ticket_messages', 'sanjab_ticket_messages');
}
/* -------------------------------- Mutators -------------------------------- */
/**
* Created at in diffrence format.
*
* @return string
*/
public function getCreatedAtDiffAttribute()
{
return ($this->created_at->diff()->d > 0 ? $this->created_at->locale(config('app.locale'), config('app.fallback_locale'), 'en')->diffForHumans().' - ' :'').$this->created_at->format('H:i');
}
/**
* Updated at in diffrence format.
*
* @return string
*/
public function getUpdatedAtDiffAttribute()
{
return ($this->updated_at->diff()->d > 0 ? $this->updated_at->locale(config('app.locale'), config('app.fallback_locale'), 'en')->diffForHumans().' - ' :'').$this->updated_at->format('H:i');
}
/**
* File link attribute.
*
* @return null|string
*/
public function getFileLinkAttribute()
{
if ($this->file) {
return Storage::disk(config('sanjab-ticket.files.disk'))->url($this->file);
}
return null;
}
}