i'm building a JSON API for E-Commerce Application
I've 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 to do is to make a single order for each shop
for example:
if user select products from 3 different Shops
instead of creating 1 order with these products,
i wanna to 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":1,
"price":4.75,
"quantity":1
},
{
"product_id":2,
"price":18.5,
"quantity":1
},
{
"product_id":3,
"price":88,
"quantity":2
}
]
any ideas to do this ?
NOTE
I made "item_count" and "grand_total" as a static data for now just for test
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire