Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Nicho's avatar
Level 1

Laravel : overwritten by the current input data

Hi. 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 way i do it, the new input data just overwrite the name and the gender, so the output is like this :

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

How can i fix it??

The Model :

class BookOther extends Model
{
    use HasFactory;
    protected $table = 'bravo_book_others';
    
    protected $fillable = [
        'booking_id',
        'user_id',
        'gender',
        'other_name',
        'other_address',
        'other_emails'
    ];
    
    public function booking(){
        return $this->belongsTo('Modules\Booking\Models\Booking', 'booking_id', 'id');
    }
    
    public function user(){
        return $this->belongsTo('App\User', 'user_id', 'id');
    }
}

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));
        }
0 likes
8 replies
piljac1's avatar

Can you post the output of dd($request->all()); if you put it at the top of your controller method please?

Nicho's avatar
Level 1

@piljac1 i don't know how to get the output. Already try the dd($request->all()); but in console and laravel debug it doesn't show anything

Nicho's avatar
Level 1

@piljac1 Here's the output :

array:19 [
  "code" => "f1951873c898cfef1201c3d29dacfc38"
  "first_name" => "Nicholas"
  "last_name" => "Kurniawan"
  "email" => "[email protected]"
  "phone" => "081287765887"
  "address_line_1" => "Jl Mangga Besar II"
  "address_line_2" => null
  "city" => null
  "city_old" => null
  "state" => null
  "state_old" => null
  "zip_code" => null
  "country" => "ID"
  "customer_notes" => null
  "other_name" => "b"
  "other_emails" => array:2 [
    0 => "[email protected]"
    1 => "[email protected]"
  ]
  "payment_gateway" => "offline_payment"
  "term_conditions" => "on"
  "coupon_code" => null
]
jlrdw's avatar

@Nicho what does your form look like?

Double check that you have the name fields correct.

Nicho's avatar
Level 1

@jlrdw For the form is like this:

@for ($i = 1; $i < $numbers; $i++)
                <div class="col-md-12">
                        <label >Name#{{$i}} </label>
                        <input type="text" class="form-control" id="other_name" name="other_name" autocomplete="off">
                    </div>
                    <div class="col-md-12">
                        <label >Email#{{$i}} </label>
                        <input type="text" class="form-control" id="other_emails" name="other_emails[]" autocomplete="off">
                    </div>
                @endfor

The other_name is not other_name[] because i get error message array to string conversion

piljac1's avatar
piljac1
Best Answer
Level 28

@Nicho What you need to do is building an array of other_users, which contains associative arrays representing each user.

@for ($i = 1; $i < $numbers; $i++)
    <div class="col-md-12">
        <label for="other_name_{{ $i }}">Name#{{$i}} </label>
        <input type="text" class="form-control" id="other_name_{{ $i }}" name="other_users[{{ $i }}][name]" autocomplete="off">
    </div>
    <div class="col-md-12">
        <label for="other_email_{{ $i }}">Email#{{$i}} </label>
        <input type="text" class="form-control" id="other_email_{{ $i }}" name="other_users[{{ $i }}][email]" autocomplete="off">
    </div>
@endfor

P.S. Apply the same logic for the gender.

foreach ($request->other_users as $otherUser){
    BookOther::create([
        'booking_id' => $booking->id,
        'user_id' => $booking->customer_id,
        'gender' => $otherUser['gender'],
        'other_name' => $otherUser['name'],
        'other_emails' => $otherUser['email'],
    ]);

    Mail::to($email)->send(new OthersEmail($isi_email1));
}
Nicho's avatar
Level 1

@piljac1 Thank you so much sir, this really help, thank you so much for the help. really appreciate it :D

Please or to participate in this conversation.