Jump to content

How to input 2 different $request->input() in foreach


Nicho

Recommended Posts

Hello, i'm currently developing a website using laravel.
 

I have added a 3 new columns for bravo_book_others table the columns are other_name, gender, and other_address. I have a case like this : User1 want to buy 4 holiday package for user1 friends, so user1 have to input 3 of user1 friends like this :

Gender#1 : male Name#1 : a Email#1 : a@gmail.com Address#1 : a street

Gender#2 : female Name#2 : b Email#2 : b@gmail.com Address#2 : b street

Gender#3 : male Name#3 : c Email#3 : c@gmail.com Address#3 : c street

I already done it with the email in the controller like this :

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

How can i put the other_name, gender, and other_address and store it into the database??

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, 'other_emails'=>$email ]); Mail::to($email)->send(new OthersEmail($isi_email1)); }

 

Edited by Nicho
Link to comment
Share on other sites

On 12/10/2022 at 1:30 AM, requinix said:

Unless I'm missing something, you need to add the values for the new columns into the ::create array. And presumably those values are somewhere in the $request.

I already try it like this
foreach

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

But the output will be likt this :
input :
Name : a
Email : a@gmail.com

Name : b
Email : b@gmail.com

Output :
Name : b
Email : a@gmail.com

Name : b
Email : b@gmail.com

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.