I’m 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){
$numbers = $request->numbers;
$emails = $request->email;
for($count = 0; $count < count($numbers); $count++){
emails::create([
'email'=>$emails[$count]
]);
}
return redirect ('/home/');
}
The blade file :
<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>
For example input is like this :
How many book : 5
Email number 1 : [email protected]
Email number 2 : [email protected]
Email number 3: [email protected]
Email number 4: [email protected]
2
Answers
If
$request->number
is an integer and a string specifying a number, you can use:If it’s an array and to be sure:
It works better to associate the email table with the user table.
User model should have many to many relation with booking_emails table. After this, you can use attach() method. You don’t need the loop you use to save emails. attaching / detaching
Then you can report:
If you work with array, you can get this informations but its not gonna be better solution/performance.