Giter Club home page Giter Club logo

reatable's Introduction

Laravel Rateable

Build Status SensioLabsInsight Latest Stable Version License

Total Downloads Monthly Downloads Daily Downloads

Provides a trait to allow rating of any Eloquent models within your app for Laravel 6.

Ratings could be fivestar style, or simple +1/-1 style.

Compatability

Laravel versions < 6.x should use the 1.x releases While 2.x release may at least temporarily work with most recent laravel 5.x versions, any new development will be explicitly focused on laravel 6.x compatibility and breaking changes may occur, therefore, installation of 2.x releases with laravel versions < 6.x is not recommended, and done at your own peril.

Installation

Require the package using Composer:

composer require mo7sin/rateable

As with most Laravel packages, if you're using Laravel 5.5 or later, the package will be auto-discovered (learn more if this is new to you).

If you're using a version of Laravel before 5.5, you'll need to register the Rateable service provider. In your config/app.php add 'willvincent\Rateable\RateableServiceProvider' to the end of the $providers array.

'providers' => [

    Illuminate\Foundation\Providers\ArtisanServiceProvider::class,
    Illuminate\Auth\AuthServiceProvider::class,
    ...
    willvincent\Rateable\RateableServiceProvider::class,

],

Getting started

After the package is correctly installed, you need to generate the migration.

php artisan rateable:migration

It will generate the <timestamp>_create_ratings_table.php migration. You may now run it with the artisan migrate command:

php artisan migrate

After the migration, one new table will be present, ratings.

Usage

In order to mark a model as "rateable", import the Rateable trait.

<?php namespace App;

use willvincent\Rateable\Rateable;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use Rateable;

}

Now, your model has access to a few additional methods.

First, to add a rating to your model:

$post = Post::first();

$rating = new willvincent\Rateable\Rating;
$rating->rating = 5;
$rating->user_id = \Auth::id();

$post->ratings()->save($rating);

dd(Post::first()->ratings);

Once a model has some ratings, you can fetch the average rating:

$post = Post::first();

dd($post->averageRating);
// $post->averageRating() also works for this.

Also, you can fetch the rating percentage. This is also how you enforce a maximum rating value.

$post = Post::first();

dd($post->ratingPercent(10)); // Ten star rating system
// Note: The value passed in is treated as the maximum allowed value.
// This defaults to 5 so it can be called without passing a value as well.

// $post->ratingPercent(5) -- Five star rating system totally equivilent to:
// $post->ratingPercent()

You can also fetch the sum or average of ratings for the given rateable item the current (authorized) has voted/rated.

$post = Post::first();

// These values depend on the user being logged in,
// they use the Auth facade to fetch the current user's id.


dd($post->userAverageRating); 

dd($post->userSumRating);

reatable's People

Contributors

willvincent avatar mattstauffer avatar mo7sin avatar mkwsra avatar nghtstr avatar zoxta avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.