在视图编辑上更新表列

时间:2018-03-27 08:26:10

标签: php laravel

我正在使用Laravel,并且当用户正在编辑他之前的输入时,我试图获取表更新列值(就像他搞砸了oops并且必须编辑他的订单)。

我的桌子:

材料:

| id | name | count_all | count_current | price_unit |

订单:

| id | count_order | material_id | invoice_id |

我的OrderController更新功能:

public function update(Request $request, order $order){
    if(isset($_POST['delete'])){
        $order->delete();
        return redirect('/order');
    }
    else{
        $order->material_id = $request->material_id;
        $order->invoice_id = $request->invoice_id;
        $order->count_order = $request->count_order;

        $count_order = $request->input('count_order');
        $query = $order->save();
        if($query){
            if($count_order > $request->count_order){
                DB::table('materials')
                    ->where('id', $request->material_id)
                    ->increment('count_all', ($count_order - $request->count_order));
                DB::table('materials')
                    ->where('id', $request->material_id)
                    ->increment('count_current', ($count_order - $request->count_order));
            }
            elseif($count_order < $request->count_order){
                DB::table('materials')
                    ->where('id', $request->material_id)
                    ->decrement('count_all', ($request->count_order - $count_order));
                DB::table('materials')
                    ->where('id', $request->material_id)
                    ->decrement('count_current', ($request->count_order - $count_order));                    
            }
        }
        return redirect('/order');
   }
}

我做错了什么?由于我为订单表编写了类似的插入函数,因此它更新了 Materials count_all count_current 。现在我想在上面的代码中做同样的事情,但是当有人编辑那个顺序时(如果我的问题不清楚可以写一个例子)。

修改
示例我想要实现的目标,并清楚地编辑现有的订单工作 如果有人为20笔添加订单,我们会有一个看起来像这样的订单:

| 1 (12345 - invoice_id) | 1 (Pen - material_id - which will correspond Pens id from Materials table) | 20 (count_order) |

因此,当订单生成时,它会更新材料表中的值,即:

| id = 1 | Pen | count_all + 20 | count_current + 20 | 1$ |

这意味着当您下订单时, count_all count_current 会随着 count_order 的值而增加。现在当我编辑我的订单因为我犯了一个错误并且我想订购15笔不是20时它应该相应地从材料表中减去(或者如果值更大),那么列中的值将是对。

处理Controller中插入的函数是:

public function insert(Request $request){

    $order = new order;

    $order->material_id = $request->material_id;
    $order->invoice_id = $request->invoice_id;
    $order->count_order = $request->count_order;

    $query = $order->save();
    if($query){
        DB::table('materials')
            ->where('id', $request->material_id)
            ->increment('count_all', $request->count_order);
        DB::table('materials')
            ->where('id', $request->material_id)
            ->increment('count_current', $request->count_order);
    }
    return redirect('/order');
}

1 个答案:

答案 0 :(得分:1)

这是简化版。首先测试是否同时更新订单和材料表

public function update(Request $request, $id){
    if(isset($_POST['delete'])){
        $order->delete();
        return redirect('/order');
    }
    else{
        $order = Order::findOrFail($id);
        $order->material_id = $request->material_id;
        $order->invoice_id = $request->invoice_id;
        $order->count_order = $request->count_order;
        $order->save();

        $materialID = $request->material_id;
        $countOrder = $request->count_order;

        DB::table('materials')
            ->where('id', $materialID)
            ->increment('count_all', $countOrder);
        DB::table('materials')
            ->where('id', $materialID )
            ->increment('count_current', $countOrder);     

        return redirect('/order');
   }
}