phplaravellaravel-7

Laravel redirecting back using withErrors but can't display error


So I have a user interface where users can upload videos. I have a validation rule to prevent too big videos. But if the video is even bigger than the post_max_size is giving an error screen before reaching the formrequest class, and I don't want that. I just want to inform the user, that ,,Hello, this video is too big" . So I went to my Handler.php class and made changes to the render function like this:

public function render($request, Throwable $exception)
{
    if ($exception instanceof PostTooLargeException)
    {
        return back()->withErrors(['message' => 'Too big file']);
    }

    return parent::render($request, $exception);
}

It returns me back to the previous url but fails to inform about error, and that's my problem.

I tried

@error('message')

and

 @foreach($errors as $error)
        {{$error}}
 @endforeach 

even

session->get('message');

but non of these helped, it seems my error message is not there. I would be happy to have any advice on what I'm doing wrong.


Solution

  • I figured it out. I couldn't access the session from Handler.php.

    Here is a solution for the problem: Unable to access sessions in Laravel app\Exceptions\Handler.php

    But I did it another way. I've added a nullable parameter to my route, and when redirecting -if it's not already there-, I've added the error, passed it to my blade, and it works now, but note that this wont work for every occurence for an error, only on the interface / interfaces where you add this functionality.