Search code examples
phplaravelexceptionpostlaravel-9

Save POST data to exception context in Laravel


So I'm adding some data to my exceptions that get logged in my Laravel 9 application by overriding the context() method in app/Exceptions/Handler.php.

I tried to add POST data by doing the following:

if (!empty($_POST)) {
    $context['post'] = $_POST;
}

But this does not work because $_POST is always empty. If I had access to the $request I could get it from there, but I can't access it from here as far as I can tell.

I wasn't aware that post data wasn't saved in $_POST like I would have expected in Laravel. Is there any way to access this data to include it in the exception?


Solution

  • How about the \request() helper method?

        if (count($post = \request()->post())) {
            $context['post'] = $post;
        }