skip to Main Content

i am saving data to booking table, and i want to add data to participant table as many times as total_pax are there, the first one in partiipant will be created by my code , now i want to create blank entries for remaining pax inn participant table on basis of total_pax which are in booking

Here is the booking object

{"booking":{
    "package_id":4,
    "user_id":1,
    "package_name":"masti maza",
    "first_name":"shubham",
    "last_name":"pradhan",
    "email":"[email protected]",
    "total_pax":"5",
}

Now i create Booking

 $booking = $request->booking;
            $book = Booking::create($booking);

Thereafter i created a single entry in participant table

$book->Participants()->create([
                'booking_id' => $book->id,
                'first_name' => $booking['first_name'],
                'last_name' => $booking['last_name'],
                'email' => $booking['email']
            ]);

But i also want to create 4 more entries as total_pax in booking request is 5 , one is created ,
but i want to create 4 more entries in participant that will have only booking_id, because others column are nullable

2

Answers


  1. Can you loop through the total_pax object like so?

            for ($i = 0; $i < request()->post('total_pax'); $i++) {
                $book->Participants()->create([
                    'booking_id' => $book->id,
                    'first_name' => ($i === 0) ? $booking['first_name'] : null,
                    'last_name'  => ($i === 0) ? $booking['last_name']  : null,
                    'email'      => ($i === 0) ? $booking['email']      : null,
                ]);
            }
    
    Login or Signup to reply.
  2. use createMany instead of create and pass multiple modal objects on it

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search