承接 nazaryuzhyn/laravel-comments 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

nazaryuzhyn/laravel-comments

最新稳定版本:v1.1.0

Composer 安装命令:

composer require nazaryuzhyn/laravel-comments

包简介

Comments to Laravel application

README 文档

README

Comments package for Laravel.

Features

  • Simple package to use and install
  • Creating Comments
  • Approving Comments
  • Auto Approve Comments
  • Retrieving Comments

Installation

You can install the package via composer:

composer require nazaryuzhyn/laravel-comments

Publish Migration

Add migration to your project:

php artisan vendor:publish --provider="Comments\CommentsServiceProvider" --tag=migrations

After the migration has been published you can create the comments table by running the migrations:

php artisan migrate

Publish Package Configs

In your terminal type:

php artisan vendor:publish --provider="Comments\CommentsServiceProvider" --tag=config

This command that will be published file with config config/comments.php.

See the full configuration file for more information.

Register the Package

Register package service provider in providers array inside config/app.php

'providers' => [
    // ...

    'Comments\CommentsServiceProvider',

],

Usage

Enable package in models

To let your models be able to receive comments, add the Comments\Traits\Commentable trait to the model

namespace App\Models;

use Comments\Traits\Commentable;
use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    use Commentable;
    
    // ...
}

Creating Comments

To create a comment on your models, you can use the comment method. It receives the string of the comment that you want to store.

$article = Article::find(1);

$comment = $article->comment('This is a comment...');

The comment method returns the newly created comment class.

Sometimes you may also want to create comments on behalf of other users. You can do this using the commentFromUser method and pass your user model to be bound with this comment:

$user = User::find(1);

$article = Article::find(1);

$comment = $article->commentFromUser($user, 'This is a comment from someone else.');

Approving Comments

By default, all comments that you create are not approved.

To approve a single comment, you may use the approve method on the Comment model like this:

$article = Article::find(1);

$comment = $article->comments->first();

$comment->approve();

Auto Approve Comments

If you want to approve a comment for a specific user automatically you can let your model implement the following interface and property:

namespace App\Models;

use Comments\Contracts\Commentator;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements Commentator
{
    use Commenting;

    protected bool $autoCommentApproval = true;

    // ...
}

The autoCommentApproval property is intended to specify whether comments should be automatically approved, and you can either return true to mark the comment as approved or false to mark the comment as unapproved.

Retrieving Comments

The models that use the Commentable trait have access to its comments using the comments relation:

$article = Article::find(1);

// Retrieve all comments
$comments = $article->comments;

// Retrieve only approved comments
$approved = $article->comments()->approved()->get();

// Retrieve only disapproved comments
$disapproved = $article->comments()->disapproved()->get();

// Retrieve only for today comments
$commentsForToday = $article->comments()->forToday()->get();

// Retrieve only before today comments
$commentsBeforeToday = $article->comments()->beforeToday()->get();

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 29
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-04-28