---
title: "JSON API output — Likes for Laravel | Roundly"
description: "LikeResource and toLikeArray() return the like count, the viewer’s state and the per-reaction breakdown as JSON."
url: https://roundly-consulting.com/open-source/docs/likes-for-laravel/api-output
language: en
---

[All packages](https://roundly-consulting.com/open-source.md)

[Likes for Laravel](https://roundly-consulting.com/open-source/docs/likes-for-laravel.md)

# JSON API output

Return a likeable’s like data from your API without hand-rolling the shape:

```php
use RoundlyConsulting\Likes\Http\Resources\LikeResource;

return LikeResource::make($post);          // resolves the viewer like every read (actor_resolver, else auth()->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 like every other read (actor\_resolver, else auth()->user()); a guest gets liked: false. 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:

```php
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 crypto](https://roundly-consulting.com/support-us.md)

By donating, you agree to our [donation terms](https://roundly-consulting.com/donation-terms.md).

[Support our open source work (opens in a new tab)](https://donate.stripe.com/dRmeVe8FX5PF1Qd9pXcEw00) [Join us on Patreon (opens in a new tab)](https://www.patreon.com/cw/roundly)

## 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.

[Get a quote in 48 hours](https://roundly-consulting.com/contact.md) [Browse all packages](https://roundly-consulting.com/open-source/docs/likes-for-laravel.md)
