Home > Software engineering >  Laravel : overwritten by the current input data
Laravel : overwritten by the current input data

Time:12-12

So i just add a new column into the bravo_book_others table, the column is gender, other_name. And i have this case where user1 want to buy 3 package for user1 and user1's friend. The input is like this

Name : user1
Email : [email protected]

Gender : male
Name : a
Email : [email protected]

Gender : female
Name : b
Email : [email protected]

The output :

Name : user1
Email : [email protected]

Gender : male
Name : a
Email : [email protected]

Gender : female
Name : b
Email : [email protected]

But the output that i got is this :

Name : user1
Email : [email protected]

Gender : female
Name : b
Email : [email protected]

Gender : female
Name : b
Email : [email protected]

The name and the gender got overwrite, with the new input. How can i fix this??

The Controller:

foreach ($request->input('other_emails') as $email){
            BookOther::create([
                'booking_id'=>$booking->id,
                'user_id'=>$booking->customer_id,
                'gender'=>$request->input('gender'),
                'other_name'=>$request->input('other_name'),
                'other_emails'=>$email
            ]);
            Mail::to($email)->send(new OthersEmail($isi_email1));
        }

The blade :

@for ($i = 1; $i < $numbers; $i  )
                <div >
                        <label >Name#{{$i}} </label>
                        <input type="text"  id="other_name" name="other_name" autocomplete="off">
                    </div>
<!--the reason i don't user other_name[]-->
<!--Because i get error message array to string conversion-->
                    <div >
                        <label >Email#{{$i}} </label>
                        <input type="text"  id="other_emails" name="other_emails[]" autocomplete="off">
                    </div>
                @endfor

CodePudding user response:

You will have to use other_name[] otherwise only the last entered entry for other_name would be sent back in the request. For the array to string conversion error the problem is as stated. other_name is an array and it has indexes and can't be received as $request->input('other_name'). This should work.

foreach ($request->input('other_emails') as $index => $email){
   BookOther::create([
    'booking_id' => $booking->id,
    'user_id' => $booking->customer_id,
    'gender' => $request->input('gender')[$index], // i don't see you sending gender in blade snippet but should be recvd like so
    'other_name' => $request->input('other_name')[$index],
    'other_emails' => $email
   ]);

}

Another thing I have noticed is that in blade foreach loop you're starting from $i = 1 please make sure that is correctly rendering the form. Normally indexes start at 0 but I don't have full context of your case so just an observation.

  • Related