Laravel Reaction Showcase
Discover real-world examples and use cases of Laravel Reaction in action. See how different applications leverage reaction functionality to enhance user engagement, build social features, and track user interactions.
Understanding Reaction Components
Laravel Reaction provides powerful features for managing user reactions:
- HasReaction Trait: Core trait that enables models to receive reactions
- CanReact Trait: Trait that enables models to perform reactions
- Multiple Reaction Types: Support for like, dislike, love, and any custom reaction type
- Anonymous Reactions: Track reactions by device ID for non-authenticated users
- Event System: Built-in events for reaction lifecycle management
- Rich Querying: Easy methods for counting, summarizing, and filtering reactions
Social Media Post Reactions
Social Media Platform
Multiple reaction types for posts and comments
Overview
Implement a social media platform where users can react to posts with multiple reaction types (like, love, laugh, angry, sad). This example demonstrates how to use the HasReaction and CanReact traits together to create a complete reaction system with real-time updates and user feedback.
Key Features
- Multiple reaction types
- Real-time reaction updates
- Reaction summary display
- Toggle reactions
- User reaction status
Reaction Types
- Like - Basic approval
- Love - Strong positive
- Laugh - Humor reaction
- Angry - Negative reaction
- Sad - Empathy reaction
Implementation Example
use JobMetric\Reaction\HasReaction;
use JobMetric\Reaction\CanReact;
class Post extends Model
{
use HasReaction;
protected $fillable = ['title', 'content', 'user_id'];
public function author()
{
return $this->belongsTo(User::class, 'user_id');
}
}
class User extends Model
{
use CanReact;
protected $fillable = ['name', 'email'];
}
class PostController extends Controller
{
public function react(Post $post, string $reaction)
{
$user = auth()->user();
$post->toggleReaction($reaction, $user);
return response()->json([
'success' => true,
'summary' => $post->reactionSummary(),
'user_reaction' => $post->reactionTo($user)?->reaction,
]);
}
public function show(Post $post)
{
$user = auth()->user();
return view('posts.show', [
'post' => $post,
'reaction_summary' => $post->reactionSummary(),
'user_reaction' => $user ? $post->reactionTo($user) : null,
]);
}
}
Product Review Reactions
E-Commerce Review System
Helpful reactions for product reviews
Overview
Allow customers to react to product reviews, helping other customers identify helpful reviews. This system improves the shopping experience by highlighting valuable feedback and allowing users to express their agreement with review content.
Key Features
- Helpful reaction tracking
- Review ranking by reactions
- User reaction status
- Reaction count display
- Review quality indicators
Use Cases
- Product review helpfulness
- Review quality ranking
- Customer feedback aggregation
- Review moderation
- Trust building
Implementation Example
class Review extends Model
{
use HasReaction;
protected $fillable = ['product_id', 'user_id', 'rating', 'comment'];
public function product()
{
return $this->belongsTo(Product::class);
}
public function reviewer()
{
return $this->belongsTo(User::class, 'user_id');
}
}
class ReviewController extends Controller
{
public function helpful(Review $review)
{
$user = auth()->user();
$review->addReaction('helpful', $user);
return response()->json([
'success' => true,
'helpful_count' => $review->countReactions('helpful'),
]);
}
public function index(Product $product)
{
$reviews = $product->reviews()
->with(['reviewer', 'reactions'])
->get()
->map(function ($review) {
$review->helpful_count = $review->countReactions('helpful');
$review->is_helpful = auth()->user()
? $review->hasReaction('helpful', auth()->user())
: false;
return $review;
});
return view('products.reviews', [
'product' => $product,
'reviews' => $reviews,
]);
}
}
Anonymous Article Reactions
Public Content Platform
Anonymous reactions using device identifiers
Overview
Allow anonymous users to react to articles using device identifiers, perfect for public content where users don't need to log in. This approach enables engagement tracking without requiring user registration, making it ideal for news sites, blogs, and public content platforms.
Key Features
- Device-based tracking
- No authentication required
- Cookie-based device IDs
- Mixed authenticated/anonymous
- Privacy-friendly approach
Use Cases
- News article reactions
- Blog post engagement
- Public content platforms
- Media sharing sites
- Content discovery
Implementation Example
class Article extends Model
{
use HasReaction;
protected $fillable = ['title', 'content', 'published_at'];
}
class TrackDevice
{
public function handle($request, Closure $next)
{
if (!$request->hasCookie('device_id')) {
$deviceId = Str::uuid();
return $next($request)->cookie('device_id', $deviceId, 60 * 24 * 365);
}
return $next($request);
}
}
class ArticleController extends Controller
{
public function react(Article $article, string $reaction)
{
$user = auth()->user();
$deviceId = request()->cookie('device_id');
if ($user) {
$article->toggleReaction($reaction, $user);
} elseif ($deviceId) {
$article->toggleReaction($reaction, null, ['device_id' => $deviceId]);
} else {
return response()->json(['error' => 'Device identifier required'], 400);
}
return response()->json([
'success' => true,
'summary' => $article->reactionSummary(),
]);
}
}
Reaction Analytics Dashboard
Analytics & Insights
Track reaction patterns and engagement metrics
Overview
Build an analytics dashboard to track reaction patterns and engagement metrics. This system provides insights into user behavior, content performance, and reaction trends across your application.
Key Metrics
- Total reactions count
- Reactions by type
- Reactions by source
- Top reacted content
- User engagement patterns
Analytics Features
- Real-time statistics
- Trend analysis
- Content ranking
- User behavior insights
- Performance tracking
Implementation Example
use JobMetric\Reaction\Models\Reaction;
class AnalyticsController extends Controller
{
public function reactionStats()
{
$stats = [
'total_reactions' => Reaction::count(),
'by_type' => Reaction::select('reaction', DB::raw('count(*) as total'))
->groupBy('reaction')
->pluck('total', 'reaction'),
'by_source' => Reaction::select('source', DB::raw('count(*) as total'))
->whereNotNull('source')
->groupBy('source')
->pluck('total', 'source'),
'top_reactables' => Reaction::select('reactable_type', 'reactable_id', DB::raw('count(*) as total'))
->groupBy('reactable_type', 'reactable_id')
->orderBy('total', 'desc')
->take(10)
->get()
->map(function ($item) {
$model = $item->reactable_type::find($item->reactable_id);
return [
'type' => class_basename($item->reactable_type),
'id' => $item->reactable_id,
'title' => $model->title ?? 'N/A',
'reactions' => $item->total,
];
}),
];
return view('analytics.reactions', ['stats' => $stats]);
}
}
Get Started
Ready to implement reactions in your application? Check out our comprehensive documentation: