为同一订单

时间:2018-02-28 06:18:46

标签: php laravel lumen

我正在使用Database Transaction作为终端,员工可以接受该订单后执行某项操作。我不希望将相同的订单分配给不同的员工。我使用了Database transaction但问题是当员工同时接受订单时,多个员工被分配到相同的订单。 示例代码如下:

try {
        $message = [
            "status_validation" => "Input status must have value assigned",
            "unique" => "Order is assigned to other driver"
        ];
        $this->validate($request, [
            "restaurant_id" => 'required|integer|min:1',
            "order_id" => 'required|integer|min:1|unique:driver_order,order_id',
            "status" => 'required|status_validation'
        ], $message);
    } catch (\Exception $ex) {
        return response()->json([
            "status" => "422",
            "message" => $ex->response->original
        ], 422);
    }
    try {

        DB::beginTransaction();
        $assignOrderToDriver = $this->driverOrder->createDriverOrder($request);
        DB:commit();

        return response()->json([
            "status" => "200",
            "message" => "Order has been sucessfully assigned."
        ], 200);
    }
    catch (\Exception $ex) {
        return response()->json([
            "status" => "500",
            "message" => $ex->getMessage()
        ], 500);
    }

这个问题确实在我的项目中造成问题。我在这里做错了什么?谢谢。

1 个答案:

答案 0 :(得分:0)

我认为$ this-> driverOrder-> createDriverOrder()内部应该有所改变。你能分享一下这个功能的代码吗?