Installation:
composer require binafy/laravel-reactions
Publish the migration and config:
php artisan vendor:publish --provider="Binafy\Reactions\ReactionsServiceProvider" --tag="migrations"
php artisan vendor:publish --provider="Binafy\Reactions\ReactionsServiceProvider" --tag="config"
Run the migration:
php artisan migrate
Configure Reactions:
Edit config/reactions.php to define your emoji types (e.g., like, love, laugh):
'types' => [
'like' => ['emoji' => 'π', 'name' => 'Like'],
'love' => ['emoji' => 'β€οΈ', 'name' => 'Love'],
'laugh' => ['emoji' => 'π', 'name' => 'Laugh'],
],
First Use Case:
Add reactions to a model (e.g., Post):
use Binafy\Reactions\HasReactions;
class Post extends Model
{
use HasReactions;
}
React to a post in a controller:
$post = Post::find(1);
$post->react('like'); // User automatically reacts (via middleware)
Reacting to Models:
// Add a reaction
$model->react('like');
// Remove a reaction
$model->unreact('like');
// Toggle reaction
$model->toggleReaction('like');
Fetching Reactions:
// Get all reactions for a model
$reactions = $model->reactions;
// Get reaction count by type
$count = $model->reactionCount('like');
// Get users who reacted
$users = $model->reactedUsers('like');
Middleware for Automatic Reactions:
Use Binafy\Reactions\Middleware\React to auto-react based on user actions:
// In routes/web.php
Route::middleware(['auth', 'react'])->group(function () {
// Routes where reactions are auto-applied
});
Customizing Reaction Logic: Override default behavior via traits or service providers:
// In AppServiceProvider
Reactions::extend('custom', function ($model, $type) {
// Custom logic for 'custom' reaction type
});
API Integration:
// React via API
$response = $model->react('like', request()->user());
return response()->json($response);
Middleware Timing:
React middleware runs after the route handler. Ensure your logic accounts for this (e.g., avoid reacting to a model before itβs saved).store method, move the reaction logic to a created observer or use Reactions::react($model, 'like') manually.Duplicate Reactions:
canReact method in your model:
public function canReact($type)
{
return true; // Always allow
}
Performance:
php artisan cache:clear
likes_count column).Config Overrides:
config/reactions.php file is published but not merged. Ensure you copy all required keys (e.g., types, table_name) to avoid runtime errors.Testing:
React middleware in tests:
$this->actingAs($user)->withMiddleware(\Binafy\Reactions\Middleware\React::class);
Dynamic Emoji Types: Add reaction types dynamically via the config or a service provider:
Reactions::addType('custom', ['emoji' => 'π', 'name' => 'Celebrate']);
Reaction Events: Listen for reaction events to trigger side effects:
// In EventServiceProvider
protected $listen = [
\Binafy\Reactions\Events\ReactionAdded::class => [
\App\Listeners\NotifyAuthor::class,
],
];
Batch Processing:
Use Reactions::sync() to bulk-update reactions (e.g., for imports):
Reactions::sync($model, ['like', 'love']); // Sync to these types only
Localization:
Localize emoji names in config/reactions.php:
'types' => [
'like' => [
'emoji' => 'π',
'name' => trans('reactions.like'),
],
],
Debugging:
reactions table for orphaned entries if counts are incorrect.Reactions::debug() to log reaction activity (enable in config):
'debug' => env('REACTIONS_DEBUG', false),
How can I help you explore Laravel packages today?