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 )

Updated on: 01-Sep-2022

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements