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.
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
@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
]);
}
Please or to participate in this conversation.