Can you post the output of dd($request->all()); if you put it at the top of your controller method please?
Dec 11, 2022
8
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));
}
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));
}
Please or to participate in this conversation.