在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):beyondcode/laravel-comments开源软件地址(OpenSource Url):https://github.com/beyondcode/laravel-comments开源编程语言(OpenSource Language):PHP 100.0%开源软件介绍(OpenSource Introduction):Add comments to your Laravel applicationAdd the ability to associate comments to your Laravel Eloquent models. The comments can be approved and nested. $post = Post::find(1);
$post->comment('This is a comment');
$post->commentAsUser($user, 'This is a comment from someone else'); InstallationYou can install the package via composer: composer require beyondcode/laravel-comments The package will automatically register itself. You can publish the migration with: php artisan vendor:publish --provider="BeyondCode\Comments\CommentsServiceProvider" --tag="migrations" After the migration has been published you can create the media-table by running the migrations: php artisan migrate You can publish the config-file with: php artisan vendor:publish --provider="BeyondCode\Comments\CommentsServiceProvider" --tag="config" UsageRegistering ModelsTo let your models be able to receive comments, add the namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use BeyondCode\Comments\Traits\HasComments;
class Post extends Model
{
use HasComments;
...
} Creating CommentsTo create a comment on your commentable models, you can use the $post = Post::find(1);
$comment = $post->comment('This is a comment from a user.'); The comment method returns the newly created comment class. Sometimes you also might want to create comments on behalf of other users. You can do this using the $post = Post::find(1);
$comment = $post->commentAsUser($yourUser, 'This is a comment from someone else.'); Approving CommentsBy default, all comments that you create are not approved - this is just a boolean flag called To approve a single comment, you may use the $post = Post::find(1);
$comment = $post->comments->first();
$comment->approve(); Auto Approve CommentsIf you want to automatically approve a comment for a specific user (and optionally model) you can let your User model implement the following interface and method: namespace App\Models;
use BeyondCode\Comments\Contracts\Commentator;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable implements Commentator
{
/**
* Check if a comment for a specific model needs to be approved.
* @param mixed $model
* @return bool
*/
public function needsCommentApproval($model): bool
{
return false;
}
} The Retrieving CommentsThe models that use the $post = Post::find(1);
// Retrieve all comments
$comments = $post->comments;
// Retrieve only approved comments
$approved = $post->comments()->approved()->get(); Testingcomposer test ChangelogPlease see CHANGELOG for more information what has changed recently. ContributingPlease see CONTRIBUTING for details. SecurityIf you discover any security related issues, please email [email protected] instead of using the issue tracker. CreditsLicenseThe MIT License (MIT). Please see License File for more information. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论