Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

akhilmurali's avatar

How to check if request array is empty in laravel?

I have an input field in a form. Its is an array type. How can i check if the field is empty or not?

<td><input type="text" name="exam_name[]" placeholder="Exam Name" class="form-control name_list" /></td> 

When i dd($request->exan_name) it shows null. But if i didn't gave any value to the field and submits, null value is inserting into database. How can i prevent it?

0 likes
8 replies
akhilmurali's avatar

@MichalOravec When i run this code without any input, it shows "Field is not empty"

if($request->has('exam_name')) {
            dd('field is not empty.');
        } else {
            dd('field is empty.');
        }
MohamedTammam's avatar

@akhilmurali Then do

$request->validation([
	'exam_name' => 'required|array',
	'exam_name.*' => 'required|string',
]);
1 like

Please or to participate in this conversation.