Getting Started
Throw provides a fluent, readable API for conditionally throwing exceptions in Laravel applications.
Requirements
Section titled “Requirements”Throw requires PHP 8.5+.
Installation
Section titled “Installation”Install Throw with composer:
composer require cline/throwAdd the Trait
Section titled “Add the Trait”Add Throw’s trait to your custom exception classes:
use Cline\Throw\Concerns\ConditionallyThrowable;use RuntimeException;
class InvalidTokenException extends RuntimeException{ use ConditionallyThrowable;
public static function expired(): self { return new self('Token has expired'); }}Basic Usage
Section titled “Basic Usage”Now you can use fluent conditional throwing:
// Throw if condition is trueInvalidTokenException::expired()->throwIf($token->isExpired());
// Throw unless condition is trueInvalidTokenException::expired()->throwUnless($token->isValid());Next Steps
Section titled “Next Steps”- Learn about Basic Usage patterns
- Explore HTTP Responses for aborting requests
- See Integration Patterns for Laravel conventions