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

Hamad_essa's avatar

Laravel Create Products Orders for each Shop in Api

I'm building a JSON Restful API for the eCommerce app

I have the following tables

Orders:

Schema::create('orders', function (Blueprint $table) {
        $table->id();
        $table->string('order_number');
        $table->unsignedBigInteger('user_id');
        $table->enum('status', ['pending','processing','completed','decline'])->default('pending');
        $table->float('grand_total');
        $table->integer('item_count');
        $table->boolean('is_paid')->default(false);
        $table->enum('payment_method', ['cash_on_delivery'])->default('cash_on_delivery');
        $table->string('notes')->nullable();

        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->timestamps();
    });

Order Items

Schema::create('order_items', function (Blueprint $table) {
        $table->id();
        $table->unsignedBigInteger('order_id');
        $table->unsignedBigInteger('product_id');

        $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
        $table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');

        $table->float('price');
        $table->integer('quantity');

        $table->timestamps();
    });

Products

Schema::create('products', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->date('expire_date');
        $table->decimal('price');
        $table->string('cover_img')->nullable();
        $table->unsignedBigInteger('shop_id')->nullable();
        $table->foreign('shop_id')->references('id')->on('shops')->onDelete('cascade');
        $table->timestamps();
    });

Shops

Schema::create('shops', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->integer('phone');
        $table->string('address');
        $table->boolean('status')->default(false);
        $table->unsignedBigInteger('user_id')->nullable();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

        $table->timestamps();
    });

Now, I can create an order with items successfully but what I wanna do is to make a single order for each shop

for example:

if user select products from 3 different Shops

instead of creating 1 orders with these products,

I wanna create 3 orders. one for each shop.

Controller

function store(Request $request)
{

    DB::beginTransaction();
    try{

        $order = new Order();
        $order->order_number = uniqid('ORD.');
        $order->user_id = Auth::id();
        $order->item_count = 2;
        $order->grand_total = 20;
        $order->save();

        $items = json_decode($request->getContent(), true);

        foreach( $items as $item ){
            $orderItem = new OrderItem;
            $orderItem->order_id = $order->id;
            $orderItem->product_id = $item['product_id'];
            $orderItem->price = $item['price'];
            $orderItem->quantity = $item['quantity'];
            $orderItem->save();
        }
      DB::commit();
    }catch (\Exception $e ){
        DB::rollBack();
    }
    return response(['message'=>'successful']);
}

Example of the JSON Post Request in Postman

[
                    {
                     "product_id":6,
                     "Shop_id"
                     "price":10,
                     "quantity":7
                    },
                    {
                     "product_id":10,
                     "Shop_id"
                     "price":14,
                     "quantity":2
                    },
                    {
                     "product_id":5,
                     "Shop_id"
                     "price":18,
                     "quantity":5
                    }
                ]

any ideas to do this?

NOTE

I made "item_count" and "grand_total" as a static data for now just for test

0 likes
3 replies
vpanta's avatar

If I understand, you have products which may belong to multiple stores. In that case you need stores/shop table. In orders table you need foreign key shop_id. orders.id and orders.shop_id in orders table must be unique. Example:

Orders table
id    | shop_id
1	1
2	1
1	2

Then, you create order for shop_id. In this case, id in orders table can't be autoincrement, or add another field for order_id which you manually increase before insert select max(order_id) from table where shop_id. I hope I helped.

1 like
Hamad_essa's avatar

Thank You So Much. Could you please give me an example based on my code?

martinbean's avatar

@hamad_essa If this is some form of marketplace, then I’d create one order but then have some model for each shop that shows the products in the order they’re responsible for. The reason being, if I, as a customer, place an order, then I expect to then only see one order (with one order number) in my account; not multiple orders because I happened to order products from multiple vendors.

So maybe have the Order model contain the orders a customer has ordered, and then some form of “fulfilment order” that each shop gets sent instructing them what products they need to send and to who.

2 likes

Please or to participate in this conversation.