Skip to content

Conversation

@chrispage1
Copy link
Contributor

@chrispage1 chrispage1 commented Oct 11, 2025

This enhancement allows defining the exception provided to the throw_if helper as a Closure.

The closure should return either the exception to be thrown or the class type of the exception represented as a string.

This is a quality of life improvement. Take the following:

// a success will return true while a failure will return an instance of CustomResponse $apiResponse = app()->call(MakeApiCall::class); // the below will result in an error because the exception creation logic is always run throw_if($apiResponse instanceof CustomResponse, new \Exception($apiResponse->message)); // by wrapping it in a closure, it'll only be evaluated when our if is truthy throw_if($apiResponse instanceof CustomResponse, fn() => new \Exception($apiResponse->message));

I have added this functionality with testing and the ability to preserve existing functionality, such as passing parameters through to the closure. It also gets evaluated first so that the response can then be further processed, e.g. you could return an exception class type from the closure.

Thanks!

@taylorotwell
Copy link
Member

Should the docblock of throw_unless be updated? Anywhere else?

@chrispage1
Copy link
Contributor Author

Should the docblock of throw_unless be updated? Anywhere else?

Good point - I have updated throw_unless - I can't see any other implementations that require additional documentation other than that. Thanks Taylor.

@taylorotwell taylorotwell merged commit 3e8987d into laravel:12.x Oct 13, 2025
64 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants