> ## Documentation Index
> Fetch the complete documentation index at: https://docs.fetchski-attract.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Working with results

> Ids, counts and the fields you want, in one line, from any answer.

Answers are objects, not arrays, but every list in them is a Laravel Collection. So the Collection
methods you already know do the work.

```php theme={"dark"}
Attract::blogPosts()->listBlogsAll()->pluck('id');
```

```php Tinker theme={"dark"}
= Fetchski\AttractCmsLaravel\Support\PagedItems {#7249
    all: [
      "clx8m2k4p0000a1b2c3d4e5f6:en:published",
      "clx8m2k4p0001a1b2c3d4e5f6:en:published",
      "clx8m2k4p0002a1b2c3d4e5f6:en:published",
    ],
  }
```

## What an answer is made of

> [card] Objects
>
> Read a field with `->`: `$post->title`, never `['title']`. The id is `->id`.

> [card] Collections
>
> Every list, at the top or inside an item: `count()`, `pluck()`, `where()`, `map()`.

> [card] Dates
>
> Carbon: `->updatedAt->toDateString()`, `->diffForHumans()`.

> [card] Choices
>
> PHP enums. Their text is `->value`.

## The moves you'll use most

First, put the list in a variable. `collect()` fetches it once and keeps it, so each line below
reuses it instead of fetching every page again:

```php theme={"dark"}
$all = Attract::blogPosts()->listBlogsAll()->collect();
```

Then:

| You want | Write |
| - | - |
| Every id | `$all->pluck('id')` |
| How many | `$all->count()` |
| Id → title | `$all->pluck('title', 'id')` |
| Only some | `$all->where('archived', false)` |
| Find one | `$all->firstWhere('slug', 'hello-world')` |
| Newest first | `$all->sortByDesc('publishedAt')` |
| Count per year | `$all->groupBy(fn ($p) => $p->publishedAt->year)->map->count()` |
| The first few | `$all->take(10)` |

Chain them as you like: `$all->where('archived', false)->pluck('id')`.

## Just the fields you want

`map()` turns each item into a small array, so you see only what matters:

```php theme={"dark"}
$all->map(fn ($p) => [
    'id' => $p->id,
    'title' => $p->title,
    'published' => $p->publishedAt->diffForHumans(),
]);
```

## Lists inside an item

They are Collections too, and `pluck()` reaches into nested fields with a dot:

```php theme={"dark"}
$post = $all->first();

$post->postFontOverrides?->count();   // how many font overrides
$post->category?->pluck('title');     // the category's name
```

## As a plain array or JSON

```php theme={"dark"}
$post->toArray();                            // a plain array
echo json_encode($post, JSON_PRETTY_PRINT);  // the JSON
```

> [!NOTE]
>
> `toArray()` and the JSON keep the API's own names, so the id is `_id` there, while the object's is `->id`.

## One page, or all of them

> [tab] …All()
>
> `listBlogsAll()` returns every item, fetching page after page for you. Start here.

> [tab] …Pick()
>
> `listBlogsPick(fields: ['title', 'slug'])` returns every item too, but as plain arrays of its `id` and only the fields
> named: the quick way to find an id ([Find an id](https://docs.fetchski-attract.com/find-an-id.md)).

> [tab] One page
>
> `listBlogs()` returns one page. The items are in `->results`, so write
> `listBlogs()->results->pluck('id')`. How each list pages: [Paging](https://docs.fetchski-attract.com/laravel/paging.md).

## In Tinker

An `…All()` list prints its items: Tinker fetches every page to show them. On a big list, add
`->take(20)` to see the first 20 only.

> [!TIP]
>
> Answers are read-only: `$post->title = '…'` fails. To change something, call its update method,
> e.g. `blogPosts()->updateBlog(...)`.

> [card] [Next: errors](https://docs.fetchski-attract.com/laravel/errors.md)
>
> Nothing to catch, and what Laravel does with each failure.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.