Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel User Review Laravel Package

dgvai/laravel-user-review

Laravel package that adds user reviews and star/point ratings to any Eloquent model via a Reviewable trait. Users can create or update a single review per model, and admins can post one reply (Google Play–style). Includes migrations, config, rating averages & percent.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require dgvai/laravel-user-review
    

    Publish the migration and config:

    php artisan vendor:publish --provider="Dgvai\UserReview\UserReviewServiceProvider"
    

    Run migrations:

    php artisan migrate
    
  2. Apply the Trait Use the Reviewable trait on any model (e.g., Product, Service):

    use Dgvai\UserReview\Traits\Reviewable;
    
    class Product extends Model
    {
        use Reviewable;
    }
    
  3. First Use Case: Enable Reviews Configure the model in config/user-review.php:

    'models' => [
        'Product' => \App\Models\Product::class,
    ],
    

    Now users can submit reviews via a form (e.g., Blade template) targeting the model’s reviews() relationship.


Implementation Patterns

Core Workflows

  1. Submitting a Review Use the createReview() method on the reviewable model:

    $product->createReview([
        'user_id' => auth()->id(),
        'rating' => 5,
        'comment' => 'Great product!',
    ]);
    

    Validate with the package’s built-in rules (extend via ReviewRequest in app/Http/Requests).

  2. Admin Replies Admins reply via createAdminReply():

    $review->createAdminReply([
        'admin_id' => auth()->id(),
        'response' => 'Thank you for your feedback!',
    ]);
    
  3. Displaying Reviews Fetch reviews with eager loading:

    $product->load('reviews.user', 'reviews.adminReply');
    

    Loop in Blade:

    @foreach($product->reviews as $review)
        <div class="review">
            <p>Rating: {{ $review->rating }}/5</p>
            <p>{{ $review->comment }}</p>
            @if($review->adminReply)
                <p class="admin-reply">{{ $review->adminReply->response }}</p>
            @endif
        </div>
    @endforeach
    
  4. Aggregating Ratings Use the averageRating accessor:

    $avgRating = $product->averageRating; // Returns float (e.g., 4.2)
    

Integration Tips

  • Validation: Extend ReviewRequest to add custom rules (e.g., min/max ratings).
  • Authorization: Use Laravel’s gates/policies to restrict review submission (e.g., prevent duplicate reviews).
  • Notifications: Trigger events like ReviewCreated or AdminReplyAdded to notify users/admins via Observers or Listeners.
  • APIs: Serialize reviews with API resources:
    public function toArray($request)
    {
        return [
            'rating' => $this->rating,
            'comment' => $this->comment,
            'user' => $this->user->name,
            'admin_reply' => $this->adminReply?->response,
        ];
    }
    

Gotchas and Tips

Pitfalls

  1. Migration Conflicts

    • If the reviews table already exists, drop it or manually adjust the migration. The package assumes a fresh setup.
  2. Model Binding Issues

    • Ensure the reviewable model’s primary key is id (default). Custom keys may require trait overrides.
  3. Admin Reply Uniqueness

    • The package enforces one reply per review. Attempting to create a second reply silently fails. Handle this in your UI/validation.
  4. Rating Scale Assumptions

    • Defaults to 1–5 stars. Override the getRatingScale() method in your model if using a different scale (e.g., 1–10).

Debugging

  • Missing Reviews? Check if the reviewable_id and reviewable_type columns are populated in the reviews table. Use:
    $product->reviews()->withTrashed()->get(); // Include soft-deleted reviews
    
  • Admin Reply Not Showing? Verify the admin_id is set (not null) and the review_id matches. Use:
    $review->fresh()->load('adminReply');
    

Extension Points

  1. Custom Review Models Override the default Review model by binding it in the service provider:

    $this->app->bind(
        Dgvai\UserReview\Contracts\Review::class,
        App\Models\CustomReview::class
    );
    
  2. Additional Review Fields Add columns to the reviews table (e.g., ip_address, device_info) and update the migration. Extend the Review model’s $fillable.

  3. Multi-Language Support Use Laravel’s localization for review fields (e.g., comment column). Store translations in a pivot table or JSON column.

  4. Soft Deletes Enable soft deletes for reviews by adding SoftDeletes to the Review model and configuring the deleted_at column in the migration.

Config Quirks

  • Reviewable Models: The config/user-review.php must list all models using the trait. Add dynamically if needed:
    'models' => collect(config('user-review.models'))
        ->merge([App\Models\NewModel::class])
        ->toArray(),
    
  • Default Admin User: The package assumes an admin_id field. Use a middleware to set this for admin routes:
    public function handle($request, Closure $next)
    {
        if (auth()->user()->isAdmin()) {
            request()->merge(['admin_id' => auth()->id()]);
        }
        return $next($request);
    }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope