Get "PHP 8 in a Nuthshell" (Soon includes PHP 8.4)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

Add additional attributes in request in Laravel

Sometimes, you might want to attach additional attributes to the original request that has made to the Laravel application. For instance, you want to put your own check in the middleware to see if the user is permitted to access the page or not, and pass that “check” by attaching it as an additional attribute to the request.

This way you can also prevent duplicating code, where you would repeat the same code (or query) in the middleware and in the controller. For instance take following. If I want to only allow users which has is_active set to “1”, I would write the following query twice like so.

  • SomeUserMiddleware.php
public function handle($request, Closure $next)
{
    //get the user Id
    $userId = $request->route('id');

    //find the user
    $user = User::where('is_active', 1)->where('id', $userId)->first();

    //check if the logged in user exists
    if (!$user->exists()) {
        //redirect them if they don't exist
        return redirect()->route('redirectRoute');
    }

    return $next($request);
}
  • UserController.php
public function view($id)
{
    $user = User::where('is_active', 1)->where('id', $userId)->first();
    return view('users.view', ['page' => $user]);
}

As you can see, the query to fetch the user has been written twice. This is simply the waste of resources, right?

The merge method

You can re-factor the above code by calling merge method on the request and passing the $user from the middleware like so.

  • SomeUserMiddleware.php
public function handle($request, Closure $next)
{
    //get the user Id
    $userId = $request->route('id');

    //find the user
    $user = User::where('is_active', 1)->where('id', $userId)->first();

    //check if the logged in user exists
    if (!$user->exists()) {
        //redirect them if they don't exist
        return redirect()->route('redirectRoute');
    }

    //pass the attribute onto the request
    $request->merge(['user' => $user]);

    return $next($request);
}

And retrieve, let’s say, in a controller like so.

  • UserController.php
public function view($id)
{
    return view('users.view', ['user' => $request->user]);
}

The benefit of above is now we only need to execute the single query and pass it onto the request which can be re-utilized elsewhere (in our case a controller). And apart from this, this will also make controllers “thin” as an added bonus!

Learn the fundamentals of PHP 8 (including 8.1, 8.2, and 8.3), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article?

Buy me a coffee

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?