You can create a custom rule using,
php artisan make:rule CustomArrayRule
and define your own rule like following,
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class CustomArrayRule implements Rule
{
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
if($value === null){
return true;
}
// check if input is not array and if array then length shouldn't less than one
return (gettype($value) <> 'array' || count($value) < 1) ? false : true;
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'The :attribute must contain at least one array element.';
}
}
then use it in your code,
$validated = request()->validate([
'field' => [new \App\Rules\CustomArrayRule()],
]);