Laravel错误:方法保存不存在

时间:2016-12-14 09:07:35

标签: php laravel

我已经创建了一个用于将数据更新到数据库的代码,但是我得到如下错误。当我使用dd()调试代码时,值为true。 我的save()方法出了什么问题?

  

Macroable.php第74行中的BadMethodCallException:     方法保存不存在。

这是我的控制器

public function update(Request $request, $id)
{

 /*
 <---- this line is my another code ---->
 */

   $input = Input::all();
    $servicedata = $input['service_id'];
    $employeedata = $input['emp_id'];

    foreach ($servicedata as $key => $val) {
        $detailservice = DetailServiceOrder::find($request->detail_service_id);
        $detailservice->sales_order_id = $request->sales_order_id;
        $detailservice->service_id = $input['service_id'][$key];
        $detailservice->order_type = $input['order_type'][$key];
        $detailservice->select_plan = $input['select_plan'][$key];
        $detailservice->qty = $input['qty'][$key];
        $detailservice->unit_price = $input['unit_price'][$key];
        $detailservice->note = $input['note'][$key];
        $detailservice->save();
    }

    foreach ($employeedata as $key => $val) {
        $detailemployee = DetailEmployeeOrder::find($request->detail_employee_id);
        $detailemployee->sales_order_id = $request->sales_order_id;
        $detailemployee->employee_id = $input['emp_id'][$key];
        $detailemployee->mandays = $input['mandays'][$key];
        $detailemployee->save();
    }
}

3 个答案:

答案 0 :(得分:2)

您应该始终检查查询是否返回null。使用is_nullempty()

foreach ($servicedata as $key => $val) {
    $detailservice = DetailServiceOrder::find($request->detail_service_id);
    if (!is_null($detailservice)) {
        $detailservice->sales_order_id = $request->sales_order_id;
        $detailservice->service_id = $input['service_id'][$key];
        $detailservice->order_type = $input['order_type'][$key];
        $detailservice->select_plan = $input['select_plan'][$key];
        $detailservice->qty = $input['qty'][$key];
        $detailservice->unit_price = $input['unit_price'][$key];
        $detailservice->note = $input['note'][$key];
        $detailservice->save();
    }
}

foreach ($employeedata as $key => $val) {
    $detailemployee = DetailEmployeeOrder::find($request->detail_employee_id);
    if (!is_null($detailemployee)) {
        $detailemployee->sales_order_id = $request->sales_order_id;
        $detailemployee->employee_id = $input['emp_id'][$key];
        $detailemployee->mandays = $input['mandays'][$key];
        $detailemployee->save();
    }
}

答案 1 :(得分:1)

也许尝试不同的方法?

public function update(Request $request, $id)
{

 /*
 <---- this line is my another code ---->
 */

   $input = Input::all();
    $servicedata = $input['service_id'];
    $employeedata = $input['emp_id'];

    foreach ($servicedata as $key => $val) {
        DetailServiceOrder::where('detail_service_id', $request->detail_service_id)->
            update(['sales_order_id' = $request->sales_order_id, 
                    'service_id' = $input['service_id'][$key], 
                    'sales_order_id' = $request->sales_order_id, 
                    'order_type' = $input['order_type'][$key], 
                    'select_plan' = $input['select_plan'][$key], 
                    'qty' = $input['qty'][$key], 
                    'unit_price' = $input['unit_price'][$key], 
                    'note' = $input['note'][$key] ]);            
    }

    foreach ($employeedata as $key => $val) {
        DetailEmployeeOrder::where('detail_employee_id', $request->detail_employee_id)->
            update(['sales_order_id' = $request->sales_order_id,                         
                    'employee_id' = $input['emp_id'][$key], 
                    'mandays' = $input['mandays'][$key] ]);                 
    }
}

在这种情况下,您不需要检查记录是否存在,因为它没有使用save()方法。

如需更多帮助,请查看this

希望有所帮助

答案 2 :(得分:0)

$detailservice为null我想,您应该检查它是否为空

if ( ! $detailservice) continue;