orkhanahmadov /
laravel-commentable
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Orkhanahmadov\LaravelCommentable; |
||
| 4 | |||
| 5 | use Illuminate\Database\Eloquent\Model; |
||
| 6 | use Orkhanahmadov\LaravelCommentable\Models\Comment; |
||
| 7 | |||
| 8 | trait Commentable |
||
| 9 | { |
||
| 10 | public function comments() |
||
| 11 | { |
||
| 12 | return $this->morphMany(Comment::class, 'commentable'); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 13 | } |
||
| 14 | |||
| 15 | public function comment(string $comment): Comment |
||
| 16 | { |
||
| 17 | return $this->newComment(['comment' => $comment]); |
||
| 18 | } |
||
| 19 | |||
| 20 | public function commentAs(Model $user, string $comment): Comment |
||
| 21 | { |
||
| 22 | return $this->newComment([ |
||
| 23 | 'comment' => $comment, |
||
| 24 | 'user_id' => $user->getKey(), |
||
| 25 | ]); |
||
| 26 | } |
||
| 27 | |||
| 28 | private function newComment(array $data) |
||
| 29 | { |
||
| 30 | return $this->comments()->create(array_merge( |
||
| 31 | $data, |
||
| 32 | [ |
||
| 33 | 'ip_address' => request()->ip(), |
||
| 34 | 'user_agent' => request()->userAgent(), |
||
| 35 | ] |
||
| 36 | )); |
||
| 37 | } |
||
| 38 | } |
||
| 39 |