All packages
Likes for Laravel
JSON API output
Return a likeable’s like data from your API without hand-rolling the shape:
use RoundlyConsulting\Likes\Http\Resources\LikeResource;
return LikeResource::make($post); // resolves the viewer from the request user
// or build the array yourself:
$post->toLikeArray($request->user());
// => [
// 'count' => 12,
// 'viewer_state' => ['liked' => true, 'reaction' => 'love'],
// 'breakdown' => ['like' => 8, 'love' => 4],
// ]LikeResource wraps a likeable model — not a Like row — and resolves the viewer from the request user. toLikeArray() builds the same shape from reactionSummary(), so the counting logic lives in one place.
Response shape
| Key | Type | Meaning |
|---|---|---|
count | int | Total active reactions of every type. |
viewer_state.liked | bool | Whether the viewer has any active reaction. |
viewer_state.reaction | string|null | The viewer’s reaction type, or null. |
breakdown | array<string, int> | Count per reaction type. |
In a controller
React and return the fresh state in one endpoint:
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use RoundlyConsulting\Likes\Facades\Likes;
use RoundlyConsulting\Likes\Http\Resources\LikeResource;
final class PostReactionController
{
public function update(Request $request, Post $post): LikeResource
{
$data = $request->validate([
'reaction' => ['required', 'string', Rule::in(config('likes.reactions'))],
]);
Likes::actor($request->user())->as($data['reaction'])->react($post);
return LikeResource::make($post);
}
}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 cryptoBy 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.