PHP code example of mtvs / eloquent-aggregate-rating

1. Go to this page and download the library: Download mtvs/eloquent-aggregate-rating library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mtvs / eloquent-aggregate-rating example snippets



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



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();
	}

	// ...
}



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();
	}

	// ...
}



	/**
     * Return the model events that ected static function aggregateRatingEvents()
	{ 
		return [
			'saved', 'deleted',
		];
	}



	/**
     * Return the model events that ected static function aggregateRatingEvents()
	{ 
		return [
			'approved', 'suspended', 'rejected', 'deleted',
		];
	}



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();
	}

	// ...
}