Skip to content

@json blade parsing error #56331

@maduranma

Description

@maduranma

Laravel Version

12.20.0

PHP Version

8.4.7

Database Driver & Version

No response

Description

I get an error:
Unclosed '[' on line 93 does not match ')'

On this code:

@json([
    'items' => $helpers['benefit']['getAll']()->map(fn($item) => [
        'icon' => $item->icon,
        'title' => (string)$item->title,
        'description' => (string)$item->description
    ]),
    'translation' => '%:booking.benefits%'
])

This is syntactycally valid and the thing is that just by removing the description line it works:

@json([
    'items' => $helpers['benefit']['getAll']()->map(fn($item) => [
        'icon' => $item->icon,
        'title' => (string)$item->title
    ]),
    'translation' => '%:booking.benefits%'
])

I've tried replacing the description item by just

'a' => 'a'

And it throws error too, I've tried with trailing coma after title and crashes, with and without after description and a lot of things I've tried to thing of, but no lick finding a pattern.

BTW if you are wondering it's mapped instead of ->map->only because I need to cast the objects returned by the model with the __toString magic function, it's a custom object cast I made.

As a temp solution I just use this, which shows the prior is ok and works fine:

{!! json_encode([
    'items' => $helpers['benefit']['getAll']()->map(fn($item) => [
        'icon' => $item->icon,
        'title' => (string)$item->title,
        'description' => (string)$item->description
    ]),
    'translation' => '%:booking.benefits%'
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) !!}

Steps To Reproduce

Just mockup the collection I have by placing this before the @json:

@php
    $helpers['benefit']['getAll'] = function () {
        return collect([
            (object)[
                'icon' => 'test',
                'title' => 'test',
                'description' => 'test',
            ]
        ]);
    };
@endphp

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions