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

rabol's avatar
Level 14

How to get error message if user is not authorized in request

Hi

It is very useful to create a Request for validating input from a form.

One then need to implement the rule() and for errror mesages the message() merhod.

Let’s say that the authorized() return false, then the code in the method of the request is not executed.

How about getting error messages if the user is not authorized to perform the request?

0 likes
4 replies
rabol's avatar
Level 14

This is what I talk about

“ If the authorize method returns false, a HTTP response with a 403 status code will automatically be returned and your controller method will not execute.”

jlrdw's avatar
public function update(Request $request, Post $post) {
    if ($post->author !== auth()->user()->id || auth()->user()->cannot('edit posts'))
        abort(404);// or some other 
    }
    // rest of method if Auth is okay
}

The chapter on errors explains what to do.

Please or to participate in this conversation.