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

Nicho's avatar
Level 1

Laravel : store array of input

Hi i'm currently developing a website, and trying to add this feature called booking for others. If the user want to order 5 tour package, user have to input 4 emails of that user friends, to notify them that they got the tour package by this user. The idea i had is to store the emails in array. When i tried to create the front-end i got error count(): Argument #1 ($value) must be of type Countable|array, null given. How can i store the array value??

The Controller :

public function store(Request $request){
        $email = new emails();
        $email->numbers = $request->numbers;
        $email->email = $request->email;
        for($count = 1; $count < count($email->numbers); $count++){
            $data = array(
                'email'=>$email->email[$email->numbers],
            );
            $insert_email[] = $data;
        }
        emails::insert($insert_email);
        return redirect ('/home/');
    }

The blade :

@section('content')
<center>
    Hello World
</center>
<form action="{{route('EmailStore')}}" method="post">
    @csrf
    <div class="form-group">
        <label>How many people you want to send email</label>
        <input type="text" required class="form-control @error ('numbers') is-invalid @enderror" 
        id="numbers" name="numbers" autocomplete="off">
    </div>
    @php
        $email = count($numbers);
    @endphp
    @for ($i = 1; $i < $email; $i++)
        <tr>
            <td>
                <label>Email.{{$i}}</label>
                <input type="text" required class="form-control @error ('email') is-invalid @enderror" 
                id="email" name="email" autocomplete="off">
            </td>
        </tr>
    @endfor
    <button type="submit" class="btn btn-info" id="btn_submit" 
                style="background-color: #eefa69; border:none; color:black">Submit</button>
</form>
@endsection
0 likes
9 replies
AungHtetPaing__'s avatar

@Nicho

@php $email = count($numbers); @endphp

where $numbers come from? You need to use wire:model or something else to update variable dynamically. Otherwise send the form first with number input only and display second form with email input based on user entered number.

Nicho's avatar
Level 1

@AungHtetPaing__ The $numbers actually a collumn in the table, because in the controller there's $email->numbers, so i think by doing the $numbers i think it should work, this is my first time of using array method in laravel

AungHtetPaing__'s avatar

@Nicho

count(): Argument #1 ($value) must be of type Countable|array, null given

You are getting that error because $numbers is null. You didn't send $numbers from controller. Am I right? And you should make email input array. No need to save numbers to table if you loop and save email one by one(5 rows for 5 emails).

<tr>
            <td>
                <label>Email.{{$i}}</label>
                <input type="text" required class="form-control @error ('email') is-invalid @enderror" 
                id="email" name="email[]" autocomplete="off">
            </td>
        </tr>

public function store(Request $request){
        $numbers= $request->numbers;
        $emails = $request->email;
		
        for($count = 0; $count < count($numbers); $count++){
            emails::create([
				'email' => $emails[$count]
			]);
        }
        return redirect ('/home/');
    }
AungHtetPaing__'s avatar

@Nicho

Yess i didn't put the $numbers in the controller, in the controller i put $email->numbers = $request->numbers

You still don't get what I am talking. If you use variable in blade, you need to sent data from controller. So in your case

public function create() {
	$numbers = 5;
	return view('blade', compact('numbers'));
}

// in blade
count($numbers);

do you get my point now?

Ok my bad, i should made the email input array. Thank you for the correction

So count(): Argument #1 ($value) must be of type Countable|array, null given solved?

Nicho's avatar
Level 1

@AungHtetPaing__ I tried to follow it like this

The controller :

public function store(Request $request){
        $numbers = $request->numbers;
        $emails = $request->email;
        for($count = 0; $count < count($numbers); $count++){
            emails::create([
                'email'=>$emails[$count]
            ]);
        }
        return redirect ('/home/');
    }

The blade :

<form action="{{route('EmailStore')}}" method="post">
    @csrf
    <div class="form-group">
        <label>How many people</label>
        <input type="text" required class="form-control @error ('numbers') is-invalid @enderror" 
        id="numbers" name="numbers" autocomplete="off">
    </div>
    @php
        $emails = count($numbers);
    @endphp
    @for ($i = 0; $i < $emails; $i++)
        <tr>
            <td>
                <label>Email.{{$i}}</label>
                <input type="text" required class="form-control @error ('email') is-invalid @enderror" 
                id="email" name="email[]" autocomplete="off">
            </td>
        </tr>
    @endfor
    <button type="submit" class="btn btn-info" id="btn_submit" 
        style="background-color: #eefa69; border:none; color:black">Submit</button>
</form>

And still got the error count(): Argument #1 ($value) must be of type Countable|array, null given

AungHtetPaing__'s avatar

@Nicho I can't explain anymore. The error is simple your $numbers is null. So make sure it is not null. I already explained in previous replies.

Please or to participate in this conversation.