How to remove Parameter From All Request Objects at Controller Level

0 votes

I have URLs that look like:

http://example.com/api/user?id=45&name=mike&api_token=2348283
http://example.com/api/project?id=5&description=first&api_token=2348283
etc...

In my controllers, I have functions that look like:

public function user_get_endpoint(Request $request){

    $request = $request->toArray();
    return UserModel::where($request)->get()->toArray();

}

The above will currently break since the $request object contains a property called api_token which does not exist in the user table. I am using the api_token in a middleware to check for authentication.

I can manually unset the api_token property in each of my API functions by using unset($request['api_token'], but I'd like to avoid that if possible.

Is there anyway to do this application wide or at a class or controller level?

Oct 21, 2020 in Laravel by kartik
• 37,530 points
2,481 views

1 answer to this question.

0 votes

Hello @kartik,

First arrange for the middleware to run on all routes:

// routes.php
$app->middleware([
    App\Http\Middleware\Apitoken::class
]);

Then define what the middleware should do:

// src/App/Http/Middleware/Apitoken.php
<?php
namespace App\Http\Middleware;

use Closure;

class Apitoken
{
    public function handle($request, Closure $next)
    {
        unset($request['api_token']);

        return $next($request);
    }
}

Hope it helps!!

answered Oct 21, 2020 by Niroj
• 82,820 points

Related Questions In Laravel

0 votes
1 answer

How to remove composer sdk from laravel packages

Use  two blade page,one for create and other ...READ MORE

answered Oct 15, 2020 in Laravel by Niroj
• 82,820 points
307 views
0 votes
1 answer

How do I write to the console from a Laravel Controller?

Hello @kartik, This can be done with the ...READ MORE

answered Oct 29, 2020 in Laravel by Niroj
• 82,820 points
141 views
0 votes
1 answer

How to select all column name from a table in laravel?

You can get all columns name by ...READ MORE

answered Dec 2, 2020 in Laravel by Niroj
• 82,820 points
2,298 views
0 votes
1 answer

How can I pass parameter from Route to Filter in laravel?

Hii, Filters can be passed parameters, like the ...READ MORE

answered Dec 4, 2020 in Laravel by Niroj
• 82,820 points
760 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,820 points
9,830 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,820 points
1,094 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,820 points
883 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,820 points
10,966 views
0 votes
1 answer

How to change value of a request parameter in laravel?

Hello @kartik, Use merge(): $request->merge([ 'user_id' => ...READ MORE

answered Aug 10, 2020 in Laravel by Niroj
• 82,820 points
3,989 views
0 votes
1 answer

How do I write to the console from a Laravel Controller?

Hello @kartik, This can be done with the ...READ MORE

answered Aug 11, 2020 in Laravel by Niroj
• 82,820 points
1,697 views