- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to remove a parameter from all Request Objects at Controller Level in Laravel?
To get all the field values from the html form you can make use of the Request class. The class Illuminate\Http\Request; has to be included in your controller.
Example 1
This example shows the student registration form and it has fields like name, email, age and address.
<html> <head> <title>Student Form</title> </head> <body> @if (count($errors) > 0) <div class = "alert alert-danger"> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <?php echo Form::open(array('url'=>'/student')); ?> <table border = '1'> <tr> <td align = 'center' colspan = '2'>Student Registration</td> </tr> <tr> <td>Name</td> <td><?php echo Form::text('name'); ?></td> </tr> <tr> <td>Email</td> <td><?php echo Form::text('email'); ?></td> </tr> <tr> <td>Age</td> <td><?php echo Form::text('age'); ?></td> </tr> <tr> <td>Address</td> <td><?php echo Form::text('address'); ?></td> </tr> <tr> <td align = 'center' colspan = '2'><?php echo Form::submit('submit'); ?></td> </tr> </table> </body> </html>
The StudentController class is as follows −
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Student; //use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\DB; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Storage; use URL; use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Validator; use Illuminate\Validation\Rule; class StudentController extends Controller{ public function index(Request $request) { return view('student'); } public function validateform(Request $request) { print_r($request->all()); } }
Inside routes/web.php the routes are defined as follows −
use App\Http\Controllers\StudentController; Route::get('student', [StudentController::class, 'index']); Route::post('student',[StudentController::class, 'validateform']);
Output
Inside the controller we are printing all the fields entered by the user. The output will be as follows −
Enter the details inside the form and click on submit you should get below details −
Array ( [_token] => nUXVJ5adsFLADKC9RCbAJcdfYCRquy8GUkUCXtR8 [name] => Rasika Desai [email] => rasika@gmail.com [age] => 20 [address] => Pune )
The array has all the details of the input field
Example 2
Now let us try to remove one field from the request object. To do that you can make use of unset() method . It is a built in php function. Let us try to remove the age field and print the array again.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Student; use Illuminate\Support\Facades\DB; class StudentController extends Controller { public function index(Request $request) { return view('student'); } public function validateform(Request $request) { print_r($request->all()); unset($request['age']); print_r($request->all()); } }
Output
Using unset($request['age']); the age field is removed from the request object as shown in the output below -
Array( [_token] => nUXVJ5adsFLADKC9RCbAJcdfYCRquy8GUkUCXtR8 [name] => Rasika Desai [email] => rasika@gmail.com [age] => 20 [address] => Pune ) Array( [_token] => nUXVJ5adsFLADKC9RCbAJcdfYCRquy8GUkUCXtR8 [name] => Rasika Desai [email] => rasika@gmail.com [address] => Pune )
Example 3
You can make use of the except() method .In that method you need to pass the key values you want to remove .
The method returns an array with all the fields except the ones passed as arguments for the except() method.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Student; class StudentController extends Controller { public function index(Request $request) { return view('student'); } public function validateform(Request $request) { print_r($request->all()); $data = $request->except(['age']); print_r($data); } }
Output
The output of the above code is −
Array ( [_token] => nUXVJ5adsFLADKC9RCbAJcdfYCRquy8GUkUCXtR8 [name] => Rasika Desai [email] => rasika@gmail.com [age] => 20 [address] => Pune ) Array ( [_token] => nUXVJ5adsFLADKC9RCbAJcdfYCRquy8GUkUCXtR8 [name] => Rasika Desai [email] => rasika@gmail.com [address] => Pune )
Example 4
You can also make use of the $request->only() method . This method takes the keys that you want to keep and the rest are discarded.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Student; class StudentController extends Controller { public function index(Request $request) { return view('student'); } public function validateform(Request $request) { print_r($request->all()); $data = $request->only(['name', 'email']); print_r($data); } }
Output
The output of the above code is as follows −
Array ( [_token] => nUXVJ5adsFLADKC9RCbAJcdfYCRquy8GUkUCXtR8 [name] => Rasika Desai [email] => rasika@gmail.com [age] => 20 [address] => Pune ) Array ( [name] => Rasika Desai [email] => rasika@gmail.com )
- Related Articles
- How to call a controller function inside a view in Laravel 5?
- How to remove all blank objects from an Object in JavaScript?
- How to pass an array as a URL parameter in Laravel?
- How to pass a CSRF token with an Ajax request in Laravel?
- Remove all objects from the Queue in C#
- Remove all objects from the Stack in C#
- How to select all the column names from a table in Laravel?
- How to get the contents of the HTTP Request body in Laravel?
- How to remove all objects except one or few in R?
- How to select ALL children (in any level) from a parent in jQuery?
- How to remove all child nodes from a parent in jQuery?
- How to remove all elements from a Collection in another Collection
- How to remove all the elements from a set in javascript?
- How to remove all the elements from a map in JavaScript?
- How to remove all whitespace from String in Java?
