How to make Laravel (Blade) text field readonly
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Effortlessly Making Laravel Blade Text Fields Readonly with PHP Processing
Introduction
Making certain fields in your Laravel application read-only is essential for security or disabling user interaction as per your app's requirements. However, using array('disabled' => 'true') might not be the best option if you want to let PHP process the field's value. In this article, we will explain how you can achieve this without sacrificing functionality by providing a comprehensive solution.
The Solution: Laravel Blade Filters
Laravel provides the Filters feature within its view system, which is a powerful tool for creating custom filter functions to process your data before being rendered on the front-end. This allows you to modify or manipulate input without impacting PHP processing on the backend. By using filters and Laravel's built-in Form helper, we can create a read-only text field easily.
1. Create an App\Filters\ReadonlyFilter Class:
In your project's root directory, run `php artisan make:filter ReadonlyFilter` to generate the class file inside App/Filters.
2. Implement the Filter Functionality in the ReadonlyFilter Class:
Add the following code to the generated ReadonlyFilter class:
```php
';
}
}
```
This class provides a simple filter that takes the input, adds an HTML readonly attribute to make it read-only, uses Laravel's Input facade to retrieve the old input (which will be set during validation), and returns the processed text field.
3. Register the ReadonlyFilter with Laravel:
To use this filter in your application, add the following line of code to the routes/web.php file:
```php
Route::filter('readonly', 'App\Filters\ReadonlyFilter');
```
Now, any request that uses the /readonly route will apply the filter.
4. Use the ReadonlyFilter in Your Laravel Blade Template:
Let's assume you want to create a form for user registration and need a read-only field for displaying an ID number generated by your application. In your blade template, use the Form helper as follows:
```blade
{{ Form::text('id', $data['user']['id'], ['class' => 'form-control', 'readonly' => true]) }}
```
This code will generate a read-only text field with the value and class attributes, and PHP will still process the value because it is not being processed by the filter.
5. Conclusion:
In this article, we have discussed how to make Laravel (Blade) text fields read-only effectively without hindering your application's PHP processing. By using filters with Laravel's Form helper, you can achieve a secure and well-structured solution for your application needs. It's always essential to keep your website safe while ensuring user convenience and data integrity. With the help of this tutorial, your Laravel projects will be more robust and secure!