Did you know the ‘exclude’ validation rule in Laravel?

author
0 minutes, 24 seconds Read

It doesn’t necessarily validate anything but simply removes the associated field from your validated data. Very useful for fields that you don’t eventually need for fulfilling the request.

public function store (Request $request): Response
{
    $validated = $request→validate([
        'name'  => 'required',
        'terms_and_conditions' => 'accepted | exclude',
    ]);


    // You don't need this...
    User::create($request->except('terms_and_conditions'));


    // The "exclude" rule ensures that the given key
    // is not part of the return validated() key;
    User::create($validated));
}
PHP

Share To:

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *