1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Laravel :package_name. |
5
|
|
|
* |
6
|
|
|
* (c) DraperStudio <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace DraperStudio\Commentable\Models; |
13
|
|
|
|
14
|
|
|
use Illuminate\Database\Eloquent\Model; |
15
|
|
|
use Kalnoy\Nestedset\Node; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Comment. |
19
|
|
|
* |
20
|
|
|
* @author DraperStudio <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class Comment extends Node |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $guarded = ['id', 'created_at', 'updated_at']; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return bool |
31
|
|
|
*/ |
32
|
|
|
public function hasChildren() |
33
|
|
|
{ |
34
|
|
|
return $this->children()->count() > 0; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return mixed |
39
|
|
|
*/ |
40
|
|
|
public function commentable() |
41
|
|
|
{ |
42
|
|
|
return $this->morphTo(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return mixed |
47
|
|
|
*/ |
48
|
|
|
public function creator() |
49
|
|
|
{ |
50
|
|
|
return $this->morphTo('creator'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param Model $commentable |
55
|
|
|
* @param $data |
56
|
|
|
* @param Model $creator |
57
|
|
|
* |
58
|
|
|
* @return static |
59
|
|
|
*/ |
60
|
|
|
public function createComment(Model $commentable, $data, Model $creator) |
61
|
|
|
{ |
62
|
|
|
$comment = new static(); |
63
|
|
|
$comment->fill(array_merge($data, [ |
64
|
|
|
'creator_id' => $creator->id, |
65
|
|
|
'creator_type' => get_class($creator), |
66
|
|
|
])); |
67
|
|
|
|
68
|
|
|
$commentable->comments()->save($comment); |
69
|
|
|
|
70
|
|
|
return $comment; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param $id |
75
|
|
|
* @param $data |
76
|
|
|
* |
77
|
|
|
* @return mixed |
78
|
|
|
*/ |
79
|
|
|
public function updateComment($id, $data) |
80
|
|
|
{ |
81
|
|
|
$comment = static::find($id); |
82
|
|
|
$comment->update($data); |
83
|
|
|
|
84
|
|
|
return $comment; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param $id |
89
|
|
|
* |
90
|
|
|
* @return mixed |
91
|
|
|
*/ |
92
|
|
|
public function deleteComment($id) |
93
|
|
|
{ |
94
|
|
|
return static::find($id)->delete(); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.