Php Laravel使用API创建订单

Php Laravel使用API创建订单,php,json,laravel,api,laravel-5,Php,Json,Laravel,Api,Laravel 5,我正在为电子商务应用程序构建一个API 现在,我陷入了创建秩序的困境 我有以下迁移 命令 Schema::create('orders', function (Blueprint $table) { $table->id(); $table->string('order_number'); $table->unsignedBigInteger('user_id'); $table->enum(

我正在为电子商务应用程序构建一个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();
    });
订购商品

        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请求中添加项呢

例如,从邮递员发布JSON数据

有什么想法吗

UDPATE

JSON Post请求

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

目前,您正在存储硬记录而不是使用$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;
}

目前,您正在存储硬记录而不是使用$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;
}

我知道这只是为了测试我想做的是在订单上添加商品你说的在订单上添加商品是什么意思?向订单中添加新项目是一种有效的方法。我知道这只是为了测试我想做的是向订单中添加项目向订单中添加项目是什么意思?像上面这样向订单添加新行是一种工作方式。是否要使用已创建的
order
order\u项目中创建新行?@GhanuBha是,这正是我想要的,但我想使用json请求来实现这一点。如果您将json请求数据放在这里,我们会帮助您。@GhanuBha我更新代码,您可以这样做。是否要使用创建的
order
order\u items
中创建新行?@GhanuBha是,这正是我想要的,但我想使用json请求来实现这一点。如果您将json请求数据放在这里,那么我们可以帮助您。@GhanuBha我更新了您可以这样做的代码。