Laravel Star Showcase
Discover real-world examples and use cases of Laravel Star in action. See how different applications leverage star rating functionality to enhance user engagement, build trust, and track content quality.
Understanding Star Rating Components
Laravel Star provides powerful features for managing star ratings:
- HasStar Trait: Core trait that enables models to receive star ratings
- CanStar Trait: Trait that enables models to perform star ratings
- Flexible Rating Scale: Support for any rating scale (1-5, 0-10, etc.)
- Anonymous Ratings: Track ratings by device ID for non-authenticated users
- Event System: Built-in events for rating lifecycle management
- Rich Querying: Easy methods for counting, averaging, and summarizing ratings
E-Commerce Product Ratings
Product Rating System
Customer ratings for products in e-commerce
Overview
Implement a comprehensive product rating system where customers can rate products from 1 to 5 stars. This system helps build trust, improves product discovery, and provides valuable feedback to sellers. Display average ratings, rating distributions, and allow customers to update their ratings.
Key Features
- 1-5 star rating scale
- Average rating calculation
- Rating distribution display
- Update existing ratings
- Rating count tracking
Business Benefits
- Build customer trust
- Improve product discovery
- Quality feedback collection
- SEO benefits
- Conversion optimization
Implementation Example
use JobMetric\Star\HasStar;
use JobMetric\Star\CanStar;
class Product extends Model
{
use HasStar;
protected $fillable = ['name', 'description', 'price'];
}
class User extends Model
{
use CanStar;
}
class ProductController extends Controller
{
public function rate(Product $product, int $rate)
{
$user = auth()->user();
$product->addStar($rate, $user);
return response()->json([
'success' => true,
'average' => $product->starAvg(),
'count' => $product->starCount(),
'summary' => $product->starSummary(),
]);
}
public function show(Product $product)
{
$user = auth()->user();
return view('products.show', [
'product' => $product,
'average_rating' => $product->starAvg(),
'rating_count' => $product->starCount(),
'rating_summary' => $product->starSummary(),
'user_rating' => $user ? $product->getRatedValue($user) : null,
]);
}
}
Content Platform Article Ratings
Article Quality Ratings
Reader ratings for articles and blog posts
Overview
Allow readers to rate articles and blog posts to help identify high-quality content. This system enables content discovery, helps editors understand reader preferences, and provides feedback for content creators. Support both authenticated and anonymous ratings.
Key Features
- Anonymous rating support
- Rating distribution charts
- Content quality indicators
- Trending content based on ratings
- Author performance tracking
Use Cases
- Blog post ratings
- News article quality
- Content curation
- Editorial feedback
- Reader engagement
Implementation Example
class Article extends Model
{
use HasStar;
protected $fillable = ['title', 'content', 'author_id'];
}
class ArticleController extends Controller
{
public function rate(Article $article, int $rate)
{
$user = auth()->user();
$deviceId = request()->cookie('device_id');
if ($user) {
$article->addStar($rate, $user);
} elseif ($deviceId) {
$article->addStar($rate, null, ['device_id' => $deviceId]);
} else {
return response()->json(['error' => 'Device identifier required'], 400);
}
return response()->json([
'success' => true,
'average' => $article->starAvg(),
'count' => $article->starCount(),
]);
}
public function index()
{
$articles = Article::with('author')
->get()
->map(function ($article) {
$article->average_rating = $article->starAvg();
$article->rating_count = $article->starCount();
return $article;
})
->sortByDesc('average_rating');
return view('articles.index', ['articles' => $articles]);
}
}
Service Provider Ratings
Service Quality Ratings
Customer ratings for service providers
Overview
Build a service provider rating system where customers can rate restaurants, hotels, service providers, and other businesses. This helps customers make informed decisions and provides valuable feedback to business owners. Display ratings prominently and allow customers to update their ratings over time.
Key Features
- Business rating system
- Rating history tracking
- Average rating display
- Rating trend analysis
- Provider performance metrics
Service Types
- Restaurants & cafes
- Hotels & accommodations
- Service providers
- Professional services
- Local businesses
Implementation Example
class Restaurant extends Model
{
use HasStar;
protected $fillable = ['name', 'address', 'cuisine_type'];
}
class RestaurantController extends Controller
{
public function rate(Restaurant $restaurant, int $rate)
{
$user = auth()->user();
$restaurant->addStar($rate, $user);
return response()->json([
'success' => true,
'average' => $restaurant->starAvg(),
'count' => $restaurant->starCount(),
'summary' => $restaurant->starSummary(),
]);
}
public function topRated()
{
$restaurants = Restaurant::all()
->map(function ($restaurant) {
$restaurant->average_rating = $restaurant->starAvg();
$restaurant->rating_count = $restaurant->starCount();
return $restaurant;
})
->filter(fn($r) => $r->rating_count > 0)
->sortByDesc('average_rating')
->take(10);
return view('restaurants.top-rated', ['restaurants' => $restaurants]);
}
}
Get Started
Ready to implement star ratings in your application? Check out our comprehensive documentation: