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 Insert a value to a hidden input in Laravel Blade?
In Laravel Blade templates, you can insert values into hidden input fields using Blade syntax or Laravel's Form helpers. Hidden fields are commonly used to store data like CSRF tokens, user IDs, or secret keys that need to be submitted with forms but should not be visible to users.
Basic Hidden Input with Blade Syntax
The simplest way is to use standard HTML with Blade template syntax
Route Setup
<?php
Route::get('hello', function () {
return view('hello', ['mymsg' => 'Welcome to Tutorialspoint']);
});
?>
Basic Blade Template (hello.blade.php)
{{$mymsg}}
Welcome to Tutorialspoint
Adding Hidden Input with Dynamic Values
To add a hidden field with dynamic data, pass the value from your route and use Blade syntax
Updated Route
<?php
Route::get('hello', function () {
return view('hello', [
'mymsg' => 'Welcome to Tutorialspoint',
'secretKey' => '123456'
]);
});
?>
Blade Template with Hidden Input
{{$mymsg}}
<form>
<input type="hidden" value="{{$secretKey}}" name="key">
</form>
Welcome to Tutorialspoint
The hidden input field will be present in the HTML source but invisible to users
<input type="hidden" value="123456" name="key">
Using Laravel Form Helper
Laravel's Form helper provides a cleaner approach for creating hidden fields
<?php echo Form::hidden('secretkey', '878$54509'); ?>
This generates
<input name="secretkey" type="hidden" value="878$54509">
Complete Form Example
{{$mymsg}}
<?php echo Form::open(array('url'=>'/hello')); ?>
<?php echo Form::hidden('secretkey', '878$54509'); ?>
<?php echo Form::close(); ?>
Adding Attributes to Hidden Fields
You can add additional attributes like id or class to hidden fields
<?php echo Form::hidden('secretkey', '878$54509', array('id' => 'key')); ?>
Or using Blade syntax
<input type="hidden" value="{{$secretKey}}" name="key" id="secretField" class="hidden-data">
Conclusion
Hidden inputs in Laravel Blade can be created using standard HTML with Blade syntax or Laravel's Form helpers. Use {{$variable}} to insert dynamic values and always validate hidden field data on the server side for security.
