Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dans1121's avatar

Custom request data not available in Controller constructor

I'm not managing to get data that I add to the Request, in my custom middleware, in the constructor of my Controller.

My Route:

Route::group(['middleware' => ['auth', 'checkIt']], function () {

    Route::get('/items', 'ItemController@index');
    
});

My 'checkIt' middleware (the handle method):

public function handle(Request $request, Closure $next) {

    $request->merge(['custom_data' => 'hello']);

    return $next($request);
}

My Controller:

class ItemController extends Controller
{

    public function __construct(Request $request) {
        echo $request->get('custom_data'); // nothing
    }

    public function index(Request $request) {
        echo $request->get('custom_data'); // outputs 'hello'
    }

}

How can I get my 'custom_data' in the constructor?

Thanks

0 likes
5 replies
Snapey's avatar

why request as a child of request?

dans1121's avatar

@Snapey I doubled checked after your comment and turns out I was using an older Laravel version's way of doing things. I've changed my code, but I'm still not able to access the 'custom_data' value in the constructor.

Any ideas?

Thanks

_Artak_'s avatar

try this

    public function __construct(Request $request) {
        $this->middleware(function() use ($request){
            echo $request->get('custom_data'); // hello
        });
    }




2 likes
dmachado's avatar

I had the same problem as @dans1121 and the @artak solution worked fine for me, but I dont understand the usage of the middleware and the function.

The code below seems right to me

public function __construct(Request $request) {
       echo $request->get('custom_data'); // nothing
   }

Please or to participate in this conversation.