# Fix: "The values.featured image field must be an array"

**For:** the Laravel CMS team.
**Severity:** blocks saving. Any article with a featured image can't be saved, autosaved or published from the admin panel.
**Date:** 3 November 2026

## What happens

The admin panel saves an article with `PUT admin/blogs/{id}` (or `POST admin/blogs`):

```json
{
  "intent": "draft",
  "version": 4,
  "values": {
    "title": "…",
    "featuredImage": "/uploads/2026/11/cover.webp",
    "featuredImageAlt": "Woman texting from a second number",
    "featuredImageTitle": "",
    "ogImage": "",
    "ctaImage": "/uploads/2026/02/need-a-second-phone-number.webp"
  }
}
```

Laravel answers with **422**:

```json
{
  "message": "The values.featured image field must be an array. (and 1 more error)",
  "errors": {
    "values.featuredImage": ["The values.featured image field must be an array."],
    "…": ["…"]
  }
}
```

## Why

The save validation was written for the Postman example **"Create draft with Base64 images — one API call"**, where `featuredImage` is an upload object:

```json
"featuredImage": { "filename": "cover.png", "data": "data:image/png;base64,…", "alt": "Blog cover" }
```

So the rule is `values.featuredImage => nullable|array` (with `.filename` and `.data` rules underneath).

The admin panel never sends Base64 in a save. Editors upload or pick images through the media library (`POST blog/admin/media`), and the save sends the **URL the media library returned**, as a string. This matches the blog API spec and the `featured_image` column, which stores a URL string.

- Empty images work: Laravel turns `""` into `null`, and `nullable` passes.
- Any article with an image fails, because a URL string isn't an array.
- The "1 more error" is most likely the same rule on another image field (`ogImage` or `ctaImage`), or the `featuredImage.data` / `.filename` rule firing because the parent isn't an array. The full `errors` object in the 422 response shows which.

## Fix: accept both shapes

`featuredImage`, `ogImage` and `ctaImage` should each accept either:

1. **a string**: an existing media URL (`/uploads/…` or `https://…`), saved as-is, or
2. **an object**: a Base64 upload (`{filename, data, alt?, title?}`), stored in the media library first, then its URL saved,

or `null` / `""` to clear the image.

### Validation (FormRequest)

```php
public function rules(): array
{
    $rules = [
        // …existing rules
        'values.featuredImageAlt'   => ['nullable', 'string', 'max:300'],
        'values.featuredImageTitle' => ['nullable', 'string', 'max:200'],
    ];

    foreach (['featuredImage', 'ogImage', 'ctaImage'] as $field) {
        $rules += $this->imageRules("values.$field");
    }

    return $rules;
}

/** A media URL string, or a Base64 upload object. */
private function imageRules(string $key): array
{
    if (is_array($this->input($key))) {
        return [
            $key              => ['array'],
            "$key.filename"   => ['required', 'string', 'max:255'],
            "$key.data"       => ['required', 'string'],          // data URL; size/type checked when stored
            "$key.alt"        => ['nullable', 'string', 'max:300'],
            "$key.title"      => ['nullable', 'string', 'max:200'],
        ];
    }

    return [
        $key => ['nullable', 'string', 'max:1000', 'regex:#^(/uploads/\S+|https?://\S+)$#i'],
    ];
}
```

### Saving (service)

```php
/** The URL to store for an image field: an existing URL, a fresh upload's URL, or null. */
private function resolveImage(mixed $value): ?string
{
    if (is_array($value)) {
        // Same code path as POST blog/admin/media.
        $media = $this->media->storeBase64(
            filename: $value['filename'],
            dataUrl:  $value['data'],
            alt:      $value['alt'] ?? null,
            title:    $value['title'] ?? null,
        );
        return $media->url;
    }

    $value = is_string($value) ? trim($value) : '';
    return $value === '' ? null : $value;
}

// when mapping values → columns
'featured_image' => $this->resolveImage($values['featuredImage'] ?? null),
'og_image'       => $this->resolveImage($values['ogImage'] ?? null),
'cta_image'      => $this->resolveImage($values['ctaImage'] ?? null),
```

Two more things to check:

- **Autosave** (live and scheduled articles) stores the `values` object in `autosave` as JSON. Resolve any Base64 object to a URL *before* storing it, so the autosave never holds image data and the admin reads back a string.
- **Response shape**: keep returning `featured_image`, `og_image` and `cta_image` as URL strings (or `null`). The admin reads them as strings.

## How to test

```bash
# 1. String URL (what the admin sends): must save, 200
curl -X PUT "$API/admin/blogs/$ID" -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
  -d '{"intent":"draft","version":'$VERSION',"values":{"title":"Image test","content":"<p>x</p>",
       "featuredImage":"/uploads/2026/11/cover.webp","featuredImageAlt":"Cover",
       "ogImage":"","ctaImage":"/uploads/2026/02/need-a-second-phone-number.webp"}}'
#    → data.featured_image == "/uploads/2026/11/cover.webp"

# 2. Base64 object (Postman "one API call"): must still work
#    → data.featured_image is the new media URL

# 3. Clearing: "featuredImage": "" or null → data.featured_image == null

# 4. Junk: "featuredImage": "not a url" → 422 on values.featuredImage
```

After deploying, open an article with a featured image in the admin panel and click **Save draft**. It should save without the error.
