morphMany(config('comments.comment_class'), 'commentable'); } /** * Attach a comment to this model. * * @return \Illuminate\Database\Eloquent\Model */ public function comment(string $comment) { return $this->commentAsUser(auth()->user(), $comment); } /** * Attach a comment to this model as a specific user. * * @return \Illuminate\Database\Eloquent\Model */ public function commentAsUser(?Model $user, string $comment) { $commentClass = config('comments.comment_class'); $comment = new $commentClass([ 'comment' => $comment, 'is_approved' => ($user instanceof Commentator) ? ! $user->needsCommentApproval($this) : false, 'user_id' => is_null($user) ? null : $user->getKey(), 'commentable_id' => $this->getKey(), 'commentable_type' => get_class($this), ]); return $this->comments()->save($comment); } }