skip to Main Content

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


  1. 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++) {
        .
        .
        .
    
    Login or Signup to reply.
  2. It works better to associate the email table with the user table.

    id user_id email
    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search