Laravel WhenHas And WhenFilled Method Example

Do you know that Laravel has a cool built in method called whenHas. Sometimes we need to check out that the request has specific data or not. That type of task you can use Laravel whenHas to check your request key.

The whenHas method will execute the given closure if a value is present on the request, see the example code.

$request->whenHas('name', function ($input) {
    //
});

 

The whenFilled method will execute the given closure if a value is present on the request and is not empty:

$request->whenFilled('name', function ($input) {
    //
});

 

Hope it can help you.

 

#laravel #eloquent-tips