Exclude validation value

When you need to validate a field, but don't actually require it for anything e.g. 'accept terms and conditions', make use of the exclude rule. That way, the validated method won't return it...

1class StoreRequest extends FormRequest
2{
3 public function rules(): array
4 {
5 return [
6 'name' => 'required|string',
7 'email_address' => 'required|email',
8 'terms_and_conditions' => 'required|accepted|exclude',
9 ];
10 }
1class RegistrationController extends Controller
2{
3 public function store(StoreRequest $request)
4 {
5 $payload = $request->validated(); // only name and email
6 
7 $user = User::create($payload);
8 
9 Auth::login($user);
10 
11 return redirect()->route('dashboard');
12 }

Tip given by @mattkingshott

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 22 courses (477 lessons, total 38 h 20 min)
  • 2 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord