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

PolMac's avatar

Updating multiple tables

Hi,

I have created, with help from on here I have created a DB that is populating from a form on the website.

The tables I have are;

Customers Products Orders

The customers table is populating as I want and is working fine. I have a product table with 4 products and each one has its own ProdID. What I would like to do is to populate the orders table with a custID and product name(based on the user selecting from the form) under a unique auto incremeninting CustID in the customers table.

Can I do this from the same controller?

here is the booking controller

 public function store(Request $request)
    {

        $customer = new Customer;
        


        $customer->name = $request->name;
        $customer->email = $request->email;
        $customer->phone = $request->number;
        $customer->comments = $request->comments;
     

routes

Route::get('booking',
    ['as' => 'booking', 'uses' => 'BookingController@create']);
Route::post('booking',
    ['as' => 'booking_store', 'uses' => 'BookingController@store']);

The orders tables has OrderID, CustID and Product name as its columns

Pól

0 likes
5 replies
aricci's avatar

If I understand correctly, after a call to:

$customer->save();

Can you not create a new order and do:

$order->CustId = $customer->id;

...

$order->save();

to populate the CustID column? Hopefully I interpreted your question correctly.

PolMac's avatar

That's it thank you I just trying to get my head around the logic.

Would I run that inside the booking controller?

   $customer = new Customer;
   $order = new Order;



        $customer->name = $request->name;
        $customer->email = $request->email;
        $customer->phone = $request->number;
        $customer->comments = $request->comments;
    $order->CustID = $customer->id;
    $order->Productname = $customer->productname;

I have set up the relationships in the models as

customer has many orders order has one customer

thanks Pól

aricci's avatar
aricci
Best Answer
Level 1

No problem. You may need some logic to check if the customer already exists, and if not, only then create one. Also, for product name, are you passing the product ID on form submission? Because you can do something like:

 $order->Productname = Products::find($request->product_id)->name;

"name" would be the product name given in the Products model.

PolMac's avatar

thank you for the help worked a treat

Please or to participate in this conversation.