Laravel Blade: htmlspecialchars(): Argument #1 ($string) must be of type string, array given
Your error
htmlspecialchars(): Argument #1 ($string) must be of type string, array given1 more line it can showFix it fast
Most common cause, 1 of 3A translation key points at a group of strings, not at one string
Echo the leaf key you meant, or loop over the group when you really want all of its entries.
{{-- one string --}}
{{ __('messages.menu.home') }}
{{-- every entry of the group --}}
@foreach (__('messages.menu') as $key => $label)
<li>{{ $label }}</li>
@endforeachShort answer
"htmlspecialchars(): Argument #1 ($string) must be of type string, array given" means a Blade {{ }} echo received an array. Laravel passes every echo to htmlspecialchars, which on PHP 8 only takes strings. In Laravel 13 the array usually comes from a translation key that names a group, an attribute cast to array, or $errors->get(). Echo a single string instead.
Which cause do you have?
Open the view and line named in the error, and dump the type of each value echoed there. The name of the value that turns out to be an array tells you which cause below applies.
@php
dump(gettype(__('messages.menu')), gettype($user->settings));
@endphpThen find the line you see in this list and open its fix:
- 1A translation key points at a group of strings, not at one string
The failing echo is {{ __('...') }}, and the key names a nested array in a lang file - 2A model attribute cast to array or JSON is echoed directly
The echo is a model property such as {{ $user->settings }}, and the model's casts list that attribute as array, json or collection - 3$errors->get() is echoed instead of $errors->first()
The failing line prints validation errors, for example {{ $errors->get('email') }}
What does the error look like?
htmlspecialchars(): Argument #1 ($string) must be of type string, array givenTypeError: htmlspecialchars(): Argument #1 ($string) must be of type string, array given (View: /var/www/html/resources/views/layouts/app.blade.php)
Cause 1 of 3
A translation key points at a group of strings, not at one string
You seeThe failing echo is {{ __('...') }}, and the key names a nested array in a lang file
Check
The htmlspecialchars array-given error from a {{ __() }} echo means the key resolved to an array. Laravel's translator returns a string for a leaf key and the whole array for a key that names a group, so {{ __('messages.menu') }} fails when menu holds several entries.
php artisan tinker --execute="dump(__('messages.menu'))"Fix
Echo the leaf key you meant, or loop over the group when you really want all of its entries.
{{-- one string --}}
{{ __('messages.menu.home') }}
{{-- every entry of the group --}}
@foreach (__('messages.menu') as $key => $label)
<li>{{ $label }}</li>
@endforeachVerify
Reload the page. The labels render and the TypeError is gone.
php artisan view:clearConfirmed by github.com
Cause 2 of 3
A model attribute cast to array or JSON is echoed directly
You seeThe echo is a model property such as {{ $user->settings }}, and the model's casts list that attribute as array, json or collection
Check
The htmlspecialchars array-given error on a model property means Eloquent cast the column to a PHP array before Blade saw it. The array cast turns a JSON or TEXT column into an array whenever the attribute is read.
grep -n "casts\|'array'\|'json'" app/Models/User.phpFix
Echo one key of the array, or, when the whole value has to reach JavaScript, render it with Js::from, which outputs the array as properly escaped JSON for the script.
{{ $user->settings['theme'] ?? 'light' }}
<script>
const settings = {{ Js::from($user->settings) }};
</script>Verify
The page shows the single value, and the script variable holds the full object in the browser console.
php artisan tinker --execute="dump(App\Models\User::first()->settings)"Confirmed by laravel.com
Cause 3 of 3
$errors->get() is echoed instead of $errors->first()
You seeThe failing line prints validation errors, for example {{ $errors->get('email') }}
Check
The htmlspecialchars array-given error next to a validation message means get() was used: it returns every message for the field as an array, while first() returns one string.
grep -rn "errors->get(" resources/views/Fix
Use first() or the @error directive to show one message, or loop over get() to show them all.
@error('email')
<p class="error">{{ $message }}</p>
@enderror
{{-- or all of them --}}
@foreach ($errors->get('email') as $message)
<p class="error">{{ $message }}</p>
@endforeachVerify
Submit the form with an invalid email; the message appears under the field instead of the error page.
php artisan view:clearConfirmed by laravel.com
How do I stop it coming back?
- Keep lang files flat where a view echoes a key, and name group keys in the plural so they read as lists.
- Render arrays for JavaScript only through Js::from, never through a plain echo.
- Add a feature test that renders each layout once, so a view that echoes an array fails in CI.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: Spotless Laundry, EZ Estimater.