Created
January 6, 2022 22:47
-
-
Save Braunson/c80d44d42a27219097f4a859363587b7 to your computer and use it in GitHub Desktop.
Laravel macro for Request class when needing to specify whenFilled but only for query params.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Only when the query param is filled. Using whenFilled will check everything, not just query params | |
Request::macro('whenQueryFilled', function ($key, callable $callback, callable $default = null) { | |
if (! is_null($this->retrieveItem('query', $key, null))) { | |
return $callback(data_get($this->query(), $key)) ?: $this; | |
} | |
if ($default) { | |
return $default(); | |
} | |
return $this; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment