I'm building an API for e-commerce app
now, i get stuck in creating order
i have the following Migrations
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->increments('id');
$table->string('img');
$table->string('name');
$table->string('desc');
$table->integer('price');
$table->timestamps();
});
and this is Models Relationship
Order Model
public function items()
{
return $this->belongsToMany(Medicine::class, 'order_item','order_id','product_id')->withPivot('quantity','price');
}
public function user()
{
return $this->belongsTo(User::class);
}
Controller
public function store(Request $request)
{
$order = new Order();
$order->order_number = uniqid('ORD.');
$order->user_id = 1;
$order->item_count = 2;
$order->grand_total = 20;
$order->save();
return response(['message'=>'successful']);
}
Now, i can add Orders successfully .
but how to add items from JSON Request
for example by posting JSON Data from Postman
JSON Post Request
[
{
"id":1,
"product_id":4018,
"price":20,
"quantity":1
},
{
"id":2,
"product_id":4019,
"price":50,
"quantity":3
},
{
"id":3,
"product_id":4020,
"price":45,
"quantity":2
}
]
any ideas?
Update
Photo of Post Request
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire