abdullahfaqeir/laravel-rating

Manage rating column for elqouent models

v3.0 2022-03-10 16:18 UTC

This package is auto-updated.

Last update: 2024-03-10 21:00:42 UTC


README

68747470733a2f2f696d6167652e6962622e636f2f65474b5067772f69665f3031395f537461725f323739323934372e706e67         like-dislike.png         68747470733a2f2f696d6167652e6962622e636f2f6867636f38622f69665f63686576726f6e5f75705f3137333138302e706e67 68747470733a2f2f696d6167652e6962622e636f2f62414e7a45472f69665f63686576726f6e5f646f776e5f3137333137372e706e67

New Maintainer

This package is an updated and maintained fork of Laravel-rating

Laravel-Ratings

Laravel package that allows you to rate, like & dislike or vote up & down your models with a simple and clear way.
If you see this packge can help, Don't skimp on me with a star :)

Rating

include CanRate trait into your user model to apply rating functions

use AbdullahFaqeir\LaravelRating\Traits\Rate\CanRate;

class User extends Model
{
    use CanRate;

include Rateable trait to your model that will be rateable

use AbdullahFaqeir\LaravelRating\Traits\Rate\Rateable;

class Post extends Model
{
    use Rateable;

now you can rate your models as the following:

$user->rate($postModel, 5);

also you can unrate your models as the following:

$user->unrate($postModel);

// alternatively
$user->rate($postModel, -1);
// or
$user->rate($postModel, false);
// or
$user->rate($postModel, null);

get the average ratings of a model

$post->ratingsAvg();

get the total count of ratings of a model

$post->ratingsCount();

get the rated models by a user

$user->rated(); // returns a collection of rated models

Voting

include CanVote trait into your user model to apply rating functionalties

use AbdullahFaqeir\LaravelRating\Traits\Vote\CanVote;

class User extends Model
{
    use CanVote;

include Votable trait to your model that will be votable

use AbdullahFaqeir\LaravelRating\Traits\Vote\Votable;

class Post extends Model
{
    use Votable;

now you can vote your model as the following:

// up vote or +1  your model
$user->upVote($postModel);

// down vote or -1 your model
$user->downVote($postModel);

get total votes count

$postModel->votesCount();

get total up votes count

$postModel->upVotesCount();

get total down votes count

$postModel->downVotesCount();

get the up voted models by a user

$user->upVoted(); // returns a collection of up voted models

get the down voted models by a user

$user->downVoted(); // returns a collection of down voted models

get the total voted models by a user

$user->voted(); // returns a collection of total voted models;

Like & Dislike

include CanLike trait into your user model to apply like and dislike functionalties

use AbdullahFaqeir\LaravelRating\Traits\Like\CanLike;

class User extends Model
{
    use CanLike;

include Likeable trait to your model that will be likeable

use AbdullahFaqeir\LaravelRating\Traits\Like\Likeable;

class Post extends Model
{
    use Likeable;

now you can like your model as the following:

// like
$user->like($postModel);

// dislike
$user->dislike($postModel);

get total likes count

$postModel->likesCount();

get total dislikes count

$postModel->dislikesCount();

get total likes and dislikes count

$postModel->likesDislikesCount();

get the liked models by a user

$user->liked(); // return a collection of liked models;

get the disliked models by a user

$user->disliked(); // return a collection of disliked models;

get the total liked and disliked models by a user

$user->likedDisliked(); // return a collection of liked and disliked models;

Install

for laravel 9.* , 8.*

composer require abdullahfaqeir/laravel-rating

in your config/app.php

    'providers' => [
        ...
        AbdullahFaqeir\LaravelRating\LaravelRatingServiceProvider::class
    ],

    'aliases' => [
        ...
        "LaravelRating" => \AbdullahFaqeir\LaravelRating\LaravelRatingFacade::class,
    ]

You don't need this step in laravel5.5 package:discover will do the job :)

publish the migrations

php artisan vendor:publish --tag=laravelRatings

run the migrations

php artisan migrate