📊 Laravel Like — Diagrams and Flow
Entity Relationship Diagram (ERD)
How it works
- A
Usercreates zero or moreLikerows. - A
Likeis associated with one concrete model through a polymorphicmodel_id+model_typepair — that model can be aPost,Video,Article, or any otherModelwith theHasLike/HasLovetrait. - Single-active invariant: a unique constraint on
(user_id, model_id, model_type, type)means one interaction type per user per model at a time.
Interaction lifecycle
| Current | Action | Result |
|---|---|---|
| (none) | like() | LIKE row created |
| (none) | toggle() | LIKE row created |
like | toggle() | Removed — returns null |
like | dislike() | Old like deleted, new dislike created |
dislike | toggle() | Changed to like |
love | toggle() | No change — returns existing love row |
All mutations above run inside DB::transaction.
Component map
LikeManager— the single service behind all actions; binds asCSlant\LaravelLike\Contracts\LikeManager.Likemodel — the shared interaction record; ownsuser()/model()relationships and theinteraction_typeaccessor.- Facade — static proxy for convenience;
LikeandLovefacades point to the same singleton.