Form Requests
A Form Request packages authorization and validation.
What it is
A Form Request packages authorization and validation.
When to use it
Use for nontrivial create/update input.
Syntax
public function authorize(): bool {}
public function rules(): array {}Example
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StoreOrderRequest extends FormRequest
{
public function authorize(): bool { return $this->user() !== null; }
public function rules(): array
{
return ["reference" => "required|string|max:80",
"total_cents" => "required|integer|min:1|max:100000000"];
}
}Common mistakes
authorize() true only establishes this request is allowed; resource ownership still needs a policy.