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

Nicho's avatar
Level 1

Laravel : Undefined offset: 2

Hi i'm currently developing a website, and trying to implementing a feature book for others. I tried to do this with an array. So scenario is like this, user want to buy 5 tour packages for that user itself and the user friends, when input the booking form, the user also need to fill 4 of the friends email, to notify them that they got the tour package. When i tried to input : How many books : 2

email 1 : [email protected]
email 2 : [email protected]

When i tried to submit it. I got this error : Undefined offset: 2

How do i fix this??

The model :

protected $fillable = [
        'other_emails'    
    ];

The Controller :

$numbers = $booking->total_guests;
        $other_emails = $request->input('other_emails');
        for($count = 1; $count < $numbers; $count++){
            Booking::create([
                'other_emails'=>$other_emails[$count]
            ]);
        }

The blade :

@php
                $numbers = $booking->total_guests;
            @endphp
            @for ($i = 1; $i < $numbers; $i++)
                <div class="col-md-12">
                <label >Email.{{$i}} </label>
                <input type="text" id="other_emails" name="other_emails[]" autocomplete="off">
            </div>
            @endfor
0 likes
3 replies
webrobert's avatar

Sounds like a bad idea. The other emails are all in one column. Seems like each email should have its own entry and the primary email should be parent is or something.

SilenceBringer's avatar
Level 55

@nicho do you know that php array indexes start at 0, not 1?

that's why $other_emails[$count] returns Undefined index for the last one - because you missed the value with index 0

Why not just loop over $other_emails?

        foreach ($request->input('other_emails') as $email){
            Booking::create([
                'other_emails'=>$email
            ]);
        }
Nicho's avatar
Level 1

@SilenceBringer I didn't know that php array indexes start at 0, i think if i start at 1 it will work just fine. Also thank you so much sir for the help i really appreciate it :D

Please or to participate in this conversation.