承接 mtvs/eloquent-aggregate-rating 相关项目开发

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

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

mtvs/eloquent-aggregate-rating

Composer 安装命令:

composer require mtvs/eloquent-aggregate-rating

包简介

Automatic rating aggregation for Laravel Eloquent models with reviews

README 文档

README

Automatic rating aggregation for Laravel Eloquent models with reviews.

Build Status

This package aggregates the ratings' average and count of a model, which is reviewed, and updates the model on the occurance of the specified events, e.g.: after saving or deleting a review. So, it facilitates the access to these values and eliminates the problem of n+1 queries when retrieving a list of the models with their aggregate-rating values and also the need for subqueries when sorting the models based on their rating.

Setup

First, modify the database table of the model that is reviewed and add two new columns, one to store the ratings' average and another one for the ratings' count, forexample:

	$table->float('rating_average')->nullable();
	$table->unsignedInteger('rating_count')->default(0);

Obviously, there has to be a rating column on the reviews table.

Then, there're two traits. One is intended to be used in the model that is reviewed. It contains an abstract method that has to be implemented to return the relationship to the review model.

use AggregateRating\HasAggregateRating;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany

Class Item extends Model
{
	use HasAggregateRating;

	public function aggregateRatingReviews(): HasMany
	{
		return $this->reviews();
	}

	// ...
}

The other is to be used in the review model. It contains an abstract method to return the relationship to the model that is reviewed.

use AggregateRating\HasIndividualRating;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

Class Review extends Model
{
	use HasIndividualRating;

	public function aggregateRatingItem(): BelongsTo
	{
		return $this->item();
	}

	// ...
}

Triggering Events

The aggregateRatingEvents(), which is on the HasIndividualRating trait, returns the events on the review model that trigger the process of the aggregation and updating the related model. This method's default definition is as the following:

	/**
     * Return the model events that require updating the aggregate rating.
     *
     * @return array
     */
	protected static function aggregateRatingEvents()
	{ 
		return [
			'saved', 'deleted',
		];
	}

But you can overwrite it to spicify your custom events:

	/**
     * Return the model events that require updating the aggregate rating.
     *
     * @return array
     */
	protected static function aggregateRatingEvents()
	{ 
		return [
			'approved', 'suspended', 'rejected', 'deleted',
		];
	}

Customizing The Column Names

If you want to choose other names for your columns, set the following constants on your models to the corresponding values.

use AggregateRating\HasAggregateRating;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany

Class Item extends Model
{
	use HasAggregateRating;

	const RATING_AVERAGE = 'rating_average';
	const RATING_COUNT = 'rating_count';

	public function aggregateRatingReviews(): HasMany
	{
		return $this->reviews();
	}

	// ...
}
use AggregateRating\HasIndividualRating;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

Class Review extends Model
{
	use HasIndividualRating;

	const RATING = 'rating';

	public function aggregateRatingItem(): BelongsTo
	{
		return $this->item();
	}

	// ...
}

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2021-01-23

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固