Skip to content

Instantly share code, notes, and snippets.

@HazemNoor
Last active December 12, 2022 09:53
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 HazemNoor/3cd72bbc0ad006d7dc7c7e404d1d88f2 to your computer and use it in GitHub Desktop.
Save HazemNoor/3cd72bbc0ad006d7dc7c7e404d1d88f2 to your computer and use it in GitHub Desktop.
Laravel helper to create a FormRequest from a Request object
<?php
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
if (!function_exists('createFormRequest')) {
/**
* Create a FormRequest from a Request object
*
* @param Request $from
* @param class-string $to a FormRequest class string
*
* @return FormRequest|null
*/
function createFormRequest(Request $from, string $to): ?FormRequest
{
if (!is_a($to, FormRequest::class, true)) {
return null;
}
$formRequest = FormRequest::createFrom($from, new $to());
$formRequest->setContainer(app())->setRedirector(app(Redirector::class));
$formRequest->validateResolved();
return $formRequest;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment