1

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

any ideas?

UDPATE

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
    }
]
  • Do you want to create new rows within `order_items` with created `order`? – GhanuBha Jun 30 '20 at 16:54
  • @GhanuBha yes, that is exactly what i want but i want to do this using json request –  Jun 30 '20 at 17:23
  • It is very usefull if you put your json request data here then we can help you. – GhanuBha Jun 30 '20 at 17:25
  • @GhanuBha i update the code –  Jun 30 '20 at 17:30
  • https://paiza.io/projects/lG0zY04llQxHX6lWFK_hlg?language=php You can do this way. – GhanuBha Jun 30 '20 at 17:46
  • @GhanuBha this way work perfectly but how to get data from json request instead of static json data –  Jul 01 '20 at 19:52
  • @GhanuBha Now, I notice a bug with this way when i change some date in json the code doesn't work correctly and it give me "successful" but there is nothing in database –  Jul 01 '20 at 20:07
  • Have you used DB transaction rollback? – GhanuBha Jul 02 '20 at 04:10
  • @GhanuBha yes i do i used the code you sent –  Jul 02 '20 at 11:47
  • Then in the last line inside try block there is DB::commit(); ? – GhanuBha Jul 02 '20 at 13:48
  • @GhanuBha yes that's right –  Jul 02 '20 at 13:53
  • Even items are not being saved in database ?? – GhanuBha Jul 02 '20 at 13:54
  • There is spell correction in `DB::commit()` there is not one time `m` two times `m` exist. – GhanuBha Jul 02 '20 at 13:58
  • @GhanuBha if i changed the json data in $items for example "product_id" and run the function again it give me the success message but the data isn't inserted to the Database –  Jul 02 '20 at 14:03
  • i already fixed the spelling before. This is not our problem –  Jul 02 '20 at 14:05
  • Comment `DB::beginTransaction()`, `DB::rollback()`, `DB::commit()` line and try. – GhanuBha Jul 02 '20 at 14:06
  • @GhanuBha Now the Order is created successfully but the items isn't and still give me the success message –  Jul 02 '20 at 14:11
  • write `dd($e);` inside catch function. and check what is the error. – GhanuBha Jul 02 '20 at 14:19
  • @GhanuBha SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`ecom`.`order_items`, CONSTRAINT `order_items_product_id_foreign` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`) ON DELETE CASCADE) (SQL: insert into `order_items` (`order_id`, `product_id`, `price`, `quantity`, `updated_at`, `created_at`) values (72, 2025, 20, 1, 2020-07-02 14:25:43, 2020-07-02 14:25:43)) –  Jul 02 '20 at 14:30
  • there is product id issue, may be `id` is not available in product table. Please check with product id. – GhanuBha Jul 02 '20 at 14:37
  • @GhanuBha No it's available! –  Jul 02 '20 at 14:41
  • https://stackoverflow.com/a/57213523/6361075 try this answer and check you items table – GhanuBha Jul 02 '20 at 14:45

1 Answers1

0

Currently you are storing hardcorded instead of that use $request values. Like below

public function store(Request $request)
{

$order = new Order();
$order->order_number = uniqid('ORD.');
$order->user_id = $request->user_id;
$order->item_count = $request->item_count;
$order->grand_total = $request->grand_total;
$order->save();

return response(['message'=>'successful']);
}

In your postman pass value like

{
user_id:1;
item_count:2;
grand_total:20;
}
Senthurkumaran
  • 1,738
  • 2
  • 19
  • 29