-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathHasComments.php
More file actions
44 lines (39 loc) · 1.1 KB
/
HasComments.php
File metadata and controls
44 lines (39 loc) · 1.1 KB
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
<?php
namespace ProcessMaker\Traits;
use ProcessMaker\Models\Comment;
use ProcessMaker\Models\User;
trait HasComments
{
/**
* Get all comments for the model.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function comments()
{
return $this->morphMany(Comment::class, 'commentable');
}
/**
* Add a comment to the model.
*
* @param string $body
* @param int|null $userId
* @param string|null $subject
* @param string $type
* @param string|null $caseNumber
* @return Comment
*/
public function addComment(string $body, ?User $user = null, ?string $subject = null, string $type = 'LOG', ?string $caseNumber = null)
{
$comment = new Comment([
'body' => $body,
'user_id' => $user->getKey(),
'subject' => $subject,
'type' => $type,
'case_number' => $caseNumber,
'commentable_type' => get_class($this),
'commentable_id' => $this->getKey(),
]);
return $this->comments()->save($comment);
}
}