Laravel Array Length Validation Example

author
0 minutes, 30 seconds Read

Laravel Validation Array Min:

When you have to validate that an array contains at least three users, you can apply the min rule:

'users' => 'array|min:3'
PHP

Laravel Validation Array Max:

When you have to validate that an array contains more than three users, you can apply the max rule:

'users' => 'array|max:3'
PHP

Laravel Validation Array Between:

When you have to validate that an array contains at least three, but not more than ten users, you can apply the between rule:

'users' => 'array|between:3,10'
PHP

Now, you can see the controller code for an example of validation:

<?php
    
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\View\View;
use Illuminate\Http\RedirectResponse;
    
class UserController extends Controller
{
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(): View
    {
        return view('create-user');
    }
        
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request): RedirectResponse
    {
        $request->validate([
                'users' => 'array|between:3,10'
            ]);
      
        ...
            
        return back()->with('success', 'User created successfully.');
    }
}
PHP

We appreciate your support and are committed to providing you useful and informative content.
We are thankful for your ongoing support 
Share To:

Similar Posts

Leave a Reply

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