NewWe open-sourced 50+ Laravel packages
Custom AI apps, agents and automation — Roundly ConsultingRoundly
All packages
Likes for Laravel

Switching reactions

react() keeps one active reaction per actor and likeable: reacting with a new type switches the existing reaction instead of adding a second one.

Likes::actor($user)->as('love')->react($post); // facade
$user->react($post, 'love');                   // trait
$user->switchReaction($post, 'wow');           // readable alias
  • No prior reaction — behaves like like() and fires Liked.
  • Same type — no-op, returns true, no event.
  • Different type — switches the reaction and fires only ReactionChanged(from, to), never Liked or Unliked, so analytics and notifications see a clean transition. The row is switched in place, or — when the actor used the new type before — that type’s own removed row is restored and the old one removed.
  • Only removed reactions — the requested type’s row (or the newest removed row, switched to the requested type) is restored and Liked fires.

Step by step

$user->react($post, 'like');   // no prior reaction: creates it, fires Liked
$user->react($post, 'like');   // same type: no-op, returns true, no event
$user->react($post, 'love');   // switches the reaction, fires ReactionChanged('like', 'love')
$user->unlike($post, 'love');  // removes it, fires Unliked

Keep using like('love') + like('wow') when you want several simultaneous reactions per actor; use react() for the common single-reaction case. Calling react() on an actor that holds several reactions collapses them to one: the requested type stays (or the newest reaction switches to it, firing ReactionChanged), and every other active reaction is removed, firing Unliked for each. The rows are read and rewritten under a row lock in one transaction, and the events fire after it commits, so two concurrent reacts can’t both report the same change.

Show your open-source love

This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.

More ways to support, including crypto

By donating, you agree to our donation terms.

Want this built into your product?

We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.