Skip to content

Instantly share code, notes, and snippets.

@Braunson
Created January 6, 2022 22:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Braunson/c80d44d42a27219097f4a859363587b7 to your computer and use it in GitHub Desktop.
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.
<?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