【问题标题】:Laravel Create Order using APILaravel 使用 API 创建订单
【发布时间】:2020-10-20 19:45:00
【问题描述】:

我正在为电子商务应用构建 API

现在,我被困在创建订单中

我有以下迁移

订单

        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();
    });

产品

Schema::create('products', function (Blueprint $table) {
        $table->increments('id');
        $table->string('img');
        $table->string('name');
        $table->string('desc');
        $table->integer('price');
        $table->timestamps();
    });

这是模型关系

订单模式

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);
}

控制器

    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']);
}

现在,我可以成功添加订单了。

但是如何从 JSON 请求中添加项目

例如通过从 Postman 发布 JSON 数据

有什么想法吗?

UDPATE

JSON 发布请求

    [
    {
        "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
    }
]

【问题讨论】:

  • 您想在order_items 中创建新行并创建order
  • @GhanuBha 是的,这正是我想要的,但我想使用 json 请求来做到这一点
  • 如果你把你的json请求数据放在这里非常有用,我们可以帮助你。
  • @GhanuBha 我更新了代码

标签: php json laravel api laravel-5


【解决方案1】:

目前您正在存储硬编码而不是使用 $request 值。如下所示

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']);
}

在你的邮递员传递值中

{
user_id:1;
item_count:2;
grand_total:20;
}

【讨论】:

  • 我知道这只是为了测试我想做的就是添加商品到订单
  • 添加商品到订单是什么意思?像上面那样添加新订单是可行的方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-06-11
  • 2022-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多