Skip to content

Create model with user info

Sortia edited this page Mar 31, 2020 · 1 revision
class CommentRepo
{

    /**
     * @var \BookStack\Actions\Comment $comment
     */
    protected $comment;

    /**
     * CommentRepo constructor.
     * @param \BookStack\Actions\Comment $comment
     */
    public function __construct(Comment $comment)
    {
        $this->comment = $comment;
    }

    /**
     * Get a comment by ID.
     * @param $id
     * @return \BookStack\Actions\Comment|\Illuminate\Database\Eloquent\Model
     */
    public function getById($id)
    {
        return $this->comment->newQuery()->findOrFail($id);
    }

    /**
     * Create a new comment on an entity.
     * @param \BookStack\Entities\Entity $entity
     * @param array $data
     * @return \BookStack\Actions\Comment
     */
    public function create(Entity $entity, $data = [])
    {
        $userId = user()->id;
        $comment = $this->comment->newInstance($data);
        $comment->created_by = $userId;
        $comment->updated_by = $userId;
        $comment->local_id = $this->getNextLocalId($entity);
        $entity->comments()->save($comment);
        return $comment;
    }
}

Clone this wiki locally