alhawari/rateify

A simple Laravel package for adding rating functionality (1โ€“5 stars) to any model.

Installs: 1

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 1

Forks: 0

Open Issues: 0

pkg:composer/alhawari/rateify

v1.0.1 2025-11-12 07:48 UTC

This package is auto-updated.

Last update: 2026-01-12 18:34:30 UTC


README

Latest Version Total Downloads License PHP Version Laravel Version

A robust, flexible, and easy-to-use Laravel package for adding star ratings (1-5) to any Eloquent model. Perfect for product reviews, content ratings, and user feedback systems.

โœจ Features

  • Rate any Eloquent model
  • Support for rating comments
  • Prevent duplicate ratings
  • Calculate average ratings
  • Get rating counts
  • Check if a user has rated an item
  • Remove ratings
  • Fully customizable (min/max rating values)
  • RESTful API endpoints
  • Secure with built-in authentication
  • Comprehensive validation
  • Well-documented code

๐Ÿš€ Installation

  1. Install the package via Composer:
composer require alhawari/rateify
  1. Publish the configuration file (optional):
php artisan vendor:publish --provider="Alhawari\\Rateify\\RateifyServiceProvider" --tag=config
  1. Publish and run the migrations:
php artisan vendor:publish --provider="Alhawari\\Rateify\\RateifyServiceProvider" --tag=migrations
php artisan migrate

โš™๏ธ Configuration

The configuration file allows you to customize the rating system:

return [
    'max_rating' => 5,  // Maximum rating value
    'min_rating' => 1,  // Minimum rating value
];

๐Ÿ›  Usage

1. Add the Rateable Trait to Your Model

use Alhawari\Rateify\Traits\Rateable;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use Rateable;
    
    // Optional: Add custom logic to control who can rate
    public function canBeRated($userId): bool
    {
        return $this->user_id !== $userId; // Prevent users from rating their own posts
    }
}

2. Basic Usage

// Get the average rating
$averageRating = $post->averageRating();

// Get the number of ratings
$ratingCount = $post->ratingsCount();

// Check if a user has rated the post
$hasRated = $post->isRatedBy($userId);

// Get a user's rating
$userRating = $post->getUserRating($userId);

// Rate a post
$rating = $post->rate($userId, 5, 'Great post!');

// Rate using the authenticated user
$rating = $post->rateByUser(4, 'Nice!');

// Remove a rating
$removed = $post->removeRating($userId);

๐ŸŒ API Endpoints

Rate or Update a Rating

POST /rateify/rate

Parameters:

Parameter Type Required Description
model string Yes Fully qualified model class
id int Yes ID of the model to rate
value int Yes Rating value (1-5)
comment string No Optional comment with the rating

Example Response:

{
    "success": true,
    "message": "Rating saved successfully.",
    "data": {
        "rating": 5,
        "comment": "Great post!",
        "average": 4.5,
        "count": 10,
        "user_rating": 5
    }
}

Remove a Rating

DELETE /rateify/rate

Parameters:

Parameter Type Required Description
model string Yes Fully qualified model class
id int Yes ID of the model

๐Ÿ”’ Permissions

By default, all API endpoints are protected by the auth middleware, so only authenticated users can rate items.

๐Ÿงช Testing

composer test

๐Ÿค Contributing

Please see contact at alhawari.officail@gmail.com .

๐Ÿ” Security

If you discover any security related issues, please email alhawari.officail@gmail.com or use issue tracker.

๐Ÿ“ License

The MIT License (MIT). Please see License File for more information.

๐Ÿ“š Credits

โญ If you find this package useful, please consider giving it a star on GitHub.