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 >
<label>How many people</label>
<input type="text" required
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
id="email" name="email[]" autocomplete="off">
</td>
</tr>
@endfor
<button type="submit" 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]
CodePudding user response:
If $request->number
is an integer and a string specifying a number, you can use:
for ($count = 0; $count < intval($numbers); $count ) {
.
.
.
If it's an array and to be sure:
for ($count = 0; $count < count((array) $numbers); $count ) {
.
.
.
CodePudding user response:
It works better to associate the email table with the user table.
id | user_id | |
---|---|---|
1 | 1 | friend1@email |
1 | 1 | friend2@email |
1 | 2 | friend3@email |
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:
- How many people booking for friends,
- How many unique users have booked in total etc.
If you work with array, you can get this informations but its not gonna be better solution/performance.