Skip to content

Latest commit

 

History

History
49 lines (35 loc) · 1.24 KB

formrequests.md

File metadata and controls

49 lines (35 loc) · 1.24 KB

Form Requests

Out of the box, Laravel has a decent method called enum on their FormRequest object. In many cases this may be enough.

asEnum

This method works exactly like Laravel's enum method, but when you are using a backed enum and want to rely on features as Defaults or when want to pass conditional Mappers, you want to use this method.

enum Suit: int {
    use \Henzeb\Enumhancer\Concerns\Defaults;

    private const Default = self::Hearts;

    case Hearts = 1;
    case Diamonds = 2;
}

# Request: /card/?card=diamonds
$request->enum('suit', Suit::class); // returns Suit::Diamonds
$request->asEnum('suit', Suit::class); // returns Suit::Diamonds

# Request: /card/?card=
$request->enum('suit', Suit::class); // returns null
$request->asEnum('suit', Suit::class); // returns Suit::Hearts

Mappers

You can pass as many mappers as you need.

enum Suit: int {
use \Henzeb\Enumhancer\Concerns\Defaults;

    case Hearts;
    case Diamonds;
}

$request->asEnum('suit', Suit::class, ['heart'=>'hearts']);
$request->asEnum('suit', Suit::class, new SuitMapper());
$request->asEnum('suit', Suit::class, new SuitMapper(), ['heart'=>'hearts']);
$request->asEnum('suit', Suit::class, SuitMapper::class);