2017 © Pedro Peláez
 

library rateable

This package helps you to add user based rating system to your model.

image

yoeunes/rateable

This package helps you to add user based rating system to your model.

  • Thursday, February 22, 2018
  • by yoeunes
  • Repository
  • 1 Watchers
  • 9 Stars
  • 69 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 3 % Grown

The README.md

Laravel 5 Rating System

:heart: This package helps you to add user based rating system to your model., (*1)

Build Status Latest Stable Version Latest Unstable Version Build Status Scrutinizer Code Quality Code Coverage Total Downloads License , (*2)

You can install the package using composer, (*3)

$ composer require yoeunes/rateable

Then add the service provider to config/app.php. In Laravel versions 5.5 and beyond, this step can be skipped if package auto-discovery is enabled., (*4)

'providers' => [
    ...
    Yoeunes\Rateable\RateableServiceProvider::class
    ...
];

Publish the migrations file:, (*5)

$ php artisan vendor:publish --provider='Yoeunes\Rateable\RateableServiceProvider' --tag="migrations"

As optional if you want to modify the default configuration, you can publish the configuration file:, (*6)

$ php artisan vendor:publish --provider='Yoeunes\Rateable\RateableServiceProvider' --tag="config"

And create tables:, (*7)

$ php artisan migrate

Finally, add feature trait into User model:, (*8)

<?php

namespace App;

use Yoeunes\Rateable\Traits\Rateable;
use Illuminate\Database\Eloquent\Model;

class Lesson extends Model
{
    use Rateable;
}

Usage

All available APIs are listed below., (*9)

Yoeunes\Rateable\Traits\Rateable

Create a rating

$user   = User::first();
$lesson = Lesson::first();

$rating = $lesson->getRatingBuilder()
                 ->user($user) // you may also use $user->id
                 ->uniqueRatingForUsers(true) // update if already rated
                 ->rate(3);

Update a rating

$lesson = Lesson::first();

$lesson->updateRating($rating_id, $value); // rating_id and the new rating value
$lesson->updateRatingForUser($user_id, $value); // update all rating for a single user related to the lesson

Delete a rating:

$lesson = Lesson::first();
$lesson->deleteRating($rating_id); // delete a rating with the giving id
$lesson->deleteRatingsForUser($user_id); // delete all rating for a single user related to the lesson
$lesson->resetRating(); // delete all rating related to the lesson

check if a model is already rated:

$lesson->isRated();
$lesson->isRatedBy($user_id);// check if its already rated by the given user

Fetch the average rating:

$lesson->averageRating(); // get the average rating 
$lesson->averageRatingForUser($user_id); // get the average rating for a single user

get list of users who rated a model (raters):

$lesson->raters()->get();
$lesson->raters()->where('name', 'like', '%yoeunes%')->get();
$lesson->raters()->orderBy('name')->get();

get count ratings between by dates

$lesson->countRatingsByDate('2018-02-03 13:23:03', '2018-02-06 15:26:06');
$lesson->countRatingsByDate('2018-02-03 13:23:03');
$lesson->countRatingsByDate(null, '2018-02-06 15:26:06');
$lesson->countRatingsByDate(Carbon::now()->parse('01-04-2017'), Carbon::now()->parse('01-06-2017'));
$lesson->countRatingsByDate(Carbon::now()->subDays(2));

other api methods:

$lesson->countRating()
$lesson->countRatingForUser($user_id)

$lesson->totalRating()
$lesson->totalRatingForUser($user_id)

$lesson->ratingPercentage()
$lesson->positiveRatingCount()
$lesson->positiveRatingTotal()
$lesson->negativeRatingCount()
$lesson->negativeRatingTotal()

Lesson::select('lessons.*')->orderByAverageRating('asc')->get()
Lesson::select('lessons.*')->orderByAverageRating('desc')->get()

Query relations

$ratings = $user->ratings
$ratings = $user->ratings()->where('id', '>', 10)->get()

date transformer

Because we all love having to repeat less, this package allows you to define date transformers. Let's say we are using the following code a lot: $lesson->countRatingsByDate(Carbon::now()->subDays(3)). It can get a little bit annoying and unreadable. Let's solve that!, (*10)

If you've published the configuration file, you will see something like this:, (*11)

'date-transformers' => [
    // 'past24hours' => Carbon::now()->subDays(1),
    // 'past7days'   => Carbon::now()->subWeeks(1),
    // 'past14days'  => Carbon::now()->subWeeks(2),
],

They are all commented out as default. To make them available, simply uncomment them. The provided ones are serving as an example. You can remove them or add your own ones., (*12)

'date-transformers' => [
    'past3days' => Carbon::now()->subDays(3),
],

We can now retrieve the rating count like this:, (*13)

$lesson->countRatingsByDate('past3days');

License

MIT, (*14)

The Versions

22/02 2018

dev-master

9999999-dev https://github.com/yoeunes/rateable

This package helps you to add user based rating system to your model.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel laravel 5 rating rate vote rateable fivestar

05/02 2018

dev-analysis-8j9Z92

dev-analysis-8j9Z92 https://github.com/yoeunes/rateable

This package helps you to add user based rating system to your model.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel laravel 5 rating rate vote rateable fivestar

05/02 2018

dev-analysis-qJm3NQ

dev-analysis-qJm3NQ https://github.com/yoeunes/rateable

This package helps you to add user based rating system to your model.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel laravel 5 rating rate vote rateable fivestar

04/02 2018

v1.0.2

1.0.2.0 https://github.com/yoeunes/rateable

Laravel 5 package to allows multiple models to be rated.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel laravel 5 rating rate vote rateable fivestar

30/01 2018

v1.0.1

1.0.1.0 https://github.com/yoeunes/rateable

Laravel 5 package to allows multiple models to be rated with a fivestar like system.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel laravel 5 rating rate vote rateable fivestar

30/01 2018

v1.0

1.0.0.0 https://github.com/yoeunes/rateable

Laravel 5 package to allows multiple models to be rated with a fivestar like system.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel laravel 5 rating rate vote rateable fivestar