Skip to content
Advertisement

Laravel Blade Component – UTF-8 Encoding issue

I’m currently working on an application with Laravel 8 version. I have build a component named input-group which cause encoding issues that i don’t understand. The code of the component look like this :

<div class="form-group" {{ isset($attributes['style']) ? $attributes->merge(['style' => $attributes['style']]) : null }}>
    @if(isset($attributes['label']))
        <label for="{{ $attributes['id'] }}">{{ $attributes['label'] }}</label>
        <input type="text" 
               value="{{ isset($attributes['value']) ? $attributes['value'] : null }}" 
               class="form-control form-control-sm" 
               name="{{ $attributes['name'] }}" 
               id="{{ $attributes['id'] }}" 
               placeholder="{{ isset($attributes['placeholder']) ? $attributes['placeholder'] : null }}">
    @else 
        <input style="width:100%;" type="text" value="{{ isset($attributes['value']) ? $attributes['value'] : null }}" class="form-control form-control-sm" name="{{ $attributes['name'] }}" id="{{ $attributes['id'] }}" placeholder="{{ isset($attributes['placeholder']) ? $attributes['placeholder'] : null }}">
    @endif
  </div>

Here is the data that I inject into the value attributes => Inspecteur de l'Education Nationale

And here is the output that i got in my <input> : Inspecteur de l'Education Nationale

I precise that my DB (using mySQL) have the Laravel default encoding utf8mb4_unicode_ci. The request that I use to get these datas is a classic Eloquent request (I don’t manipulate the encoding)

The configuration of the mysql driver look like this :

        'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

Advertisement

Answer

Use {!! !!} instead of {{ }}

I.e :

value="{!! isset($attributes['value']) ? $attributes['value'] : null !!}"

Displaying Unescaped Data

By default, Blade {{ }} statements are automatically sent through PHP’s htmlspecialchars function to prevent XSS attacks.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement