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

Nicho's avatar
Level 1

Laravel : order_id = $booking->id

Hi i'm currently developing a website, and trying to implementing a feature book for others. I tried to do this with an array. So scenario is like this, user want to buy 5 tour packages for that user itself and the user friends, when input the booking form, the user also need to fill 4 of the friends email, to notify them that they got the tour package. I added order_id column in the booking table, the purpose of order_id is to get the id of booking table, everytime the other emails is input and save into the database, the order_id value is the same as the id of that booking. Example :

How many books : 2
Booking ID : 1

Your email : [email protected]

Email#1 : [email protected]

In the database it should be like this :

ID : 1

email : [email protected]

order_id : 1
ID : 2

email : [email protected]

order_id : 1

How can i do it??

The Model :

protected $fillable = [
        'order_id',
        'other_emails'    
    ];

The Controller :

$order_id = $booking->id;
        $numbers = $booking->total_guests;
        foreach($request->input('other_emails') as $other_emails){
            Booking::create([
                'other_emails'=>$other_emails,
                'order_id'=>$order_id,
            ]);
        }
0 likes
5 replies
SilenceBringer's avatar

@nicho it will be helpful next time to explain what's wrong with your current way

As for now, I see

I added order_id column in the booking table

But in code you get id

$order_id = $booking->id;

Should it be order_id?

Nicho's avatar
Level 1

@SilenceBringer The purpose of order_id is to get the id value of the booking, that's why the code is that way. So when i tried to get the value of other_emails, it tied with the id

The example is like this

How many bookings : 2
id : 173//this is the booking id
Your email : [email protected]

Email#1 : [email protected]

In the database :

ID : 173
email : [email protected]
order_id : 173
other_emails : NULL
ID : 174
email : null
order_id : 173
other_emails : [email protected]

The a user is booking for his friend b, with booking id 173, so when it save into the database the order_id should be 173. so that i can show the list of other_emails in id 173

Nicho's avatar
Level 1

I got the error Invalid argument supplied for foreach() Of course there's something wrong in the controller,

foreach($request->input('other_emails') as $other_emails){
            Booking::create([
                'other_emails'=>$other_emails,
                'order_id'=>$order_id,
            ]);
        }

I code it that way, so when it submit, i want it the order_id is the same as the id of the booking, when that booking is created

Like when create the booking, the id is 173, and when you submit the order_id should be 173 as well

AungHtetPaing__'s avatar

@Nicho

I suggest you to watch or learn basic first. You don't even know php array start from 0. https://laracasts.com/series/php-for-beginners-2023-edition

And for laravel you should watch https://laracasts.com/series/laravel-8-from-scratch. https://laravel.com/docs/9.x/eloquent-relationships#one-to-many

i want it the order_id is the same as the id of the booking,

that doesn't make sense booking id is auto increasement and order_id doesn't need to be the same with that id. Seem like you don't understand table relationship.

Please or to participate in this conversation.