Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to make Laravel (Blade) text field read-only?
In Laravel Blade templates, you can make text fields read-only using Laravel's Form facade or plain HTML input elements. This prevents users from editing the field while still displaying the value.
Installation: For Laravel Form facade, install the package: composer require laravelcollective/html
Setting up Routes and Views
First, let's create a basic route that passes data to our Blade template
<?php
// routes/web.php
Route::get('hello', function () {
return view('hello', ['mymsg' => 'Welcome to Tutorialspoint']);
});
?>
Create hello.blade.php in the resources/views/ folder
{{ $mymsg }}
Welcome to Tutorialspoint
Using Laravel Form Facade
The Form facade provides a clean way to create read-only text fields
<?php
echo Form::open(array('url'=>'/hello'));
echo Form::text('name', null, ['readonly']);
echo Form::close();
?>
With Additional Attributes
You can also add a default value and other attributes
<?php
echo Form::open(array('url'=>'/hello'));
echo Form::text('name', 'Default Value', ['readonly', 'class' => 'form-control']);
echo Form::close();
?>
Using Plain HTML
For more control, you can use standard HTML input elements with the readonly attribute
<form action="/hello" method="GET"> <input readonly="readonly" name="username" type="text" value="Read-only text" class="form-control"> </form>
Comparison of Methods
| Method | Syntax | Benefits |
|---|---|---|
| Form Facade | Form::text('name', null, ['readonly']) |
Laravel integration, cleaner syntax |
| Plain HTML | <input readonly="readonly"> |
More control, no dependencies |
Conclusion
Use Laravel's Form facade for consistency with Laravel conventions, or plain HTML for more granular control. Both methods effectively prevent user input while maintaining the field's visual presence in forms.
