hewehi/laravel-model-review

用户可以为任何模型创建带有评分和评论的评论

dev-master 2022-07-24 15:15 UTC

This package is auto-updated.

Last update: 2024-09-24 19:52:36 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

本包基于Jalal Uddin的美丽包Github | Linked-in | Facebook,该包允许用户在系统中的任何模型上进行带评分和评论的评论,同时用户可以对同一模型进行多次评论。

我在此基础上添加了一个新功能,该功能与仅允许用户对模型进行一次评论或更新评论的系统相一致。

我的LinkedIn

Husssein El-Hewehii

要求

  • PHP >= 7.1
  • Laravel >= 5.6

安装

使用COMPOSER

composer require hewehi/laravel-model-review

配置

导出资源(迁移和配置)

php artisan vendor:publish --provider="Hewehi\ModelReview\ModelReviewServiceProvider"

运行迁移

php artisan migrate

清除配置缓存

php artisan config:cache

用法

Reviewable特性添加到用户希望进行评论和评分的模型中。例如,对于产品模型

<?php 
namespace App;

use Hewehi\ModelReview\Reviewable;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    use Reviewable;
    ...
    ...
}

?>

创建产品的评论

描述

makeReview(object $user, int $rating , string $comment)

makeOrUpdateReview(object $user, int $rating , string $comment)

评论是可选的

返回

评论实例对象

示例

    $product = Product::find($id);
    $user = auth()->user();

    //user can add new review on this product even they have one
    $product->makeReview($user, 3, 'optional comment');

    //user can only update their review on this product or create a new one if they don't have any reviews yet
    $product->makeOrUpdateReview($user, 3, 'optional comment');

评论属性

    // Get all active reviews of the product
    $product->reviews();

    // Get neumetic review count (average)
    $product->rating;

    // Get percentage review count (average)
    $product->rating_percent;

    /**
    *   NOTE: THIS PERCENTAGE IS BASED ON 5 STAR RATING, IF YOU WANT CUSTOM STAR, USE BELLOW
    *   This is configured via the config file comes with this package: user-review.php
    *   You can also set environment variable for your systems default star count
    *
    *   (.env)  SYSTEM_RATING_STAR_COUNT=5 
    */

    $product->averageRating(10);    //percentage for 10 starrted model

    // Get rating given to the product by a user:
    $product->userRating($user);

    /**
     *  Get Filtered Review
     *  Like, get only reviews that has been given 4 stars!
     * 
    */

    $product->filter(4);

    /**
     * Get it's percentage, which can be shown in the progress bar!
     * */ 
    $product->filteredPercentage(4);      // ex: output: 75 


    /**
     *  PULLING OUT REVIEWS
     *  There are several ways you can
     *  pull out reviews of products
    */

    // Get all reviews of all products
    $reviews = Hewehi\ModelReview\Review::all();              // all reviews
    $reviews = Hewehi\ModelReview\Review::active()->get();    // all active reviews
    $reviews = Hewehi\ModelReview\Review::inactive()->get();  // all inactive reviews
    $reviews = Hewehi\ModelReview\Review::daily()->get();     // all daily reviews
    $reviews = Hewehi\ModelReview\Review::monthly()->get();   // all monthly reviews
    $reviews = Hewehi\ModelReview\Review::yearly()->get();    // all yearly reviews

    // You can also chain these methods
    $reviews = Hewehi\ModelReview\Review::monthly()->active()->get();  // get aa monthly active reviews

    // Get reviews of a product
    $product->reviews();


    /**
     *  $reviews has some attributes
     *  Let's assume we are taking the first review
    */
    $review = $reviews->first();

    /**
     *  This the model object of the traited model
     *  In our case it is product
     * 
    */

    $review->model;     //  so $review->model->name with return the $product->name
    $review->user;      //  return User model that reviewed the model

    // Get review text
    $review->review_text;

    // Get review reply
    $review->reply;

    // reply a review by admin:
    $review->reply('Thanks for being with us!');

    // making active/inactive
    $review->makeActive();
    $review->makeInactive();