3

I've been create a code for updating data to database, but I'm get error like below. when I debugging the code using dd(), the value was true. what's wrong with my save() method ?

BadMethodCallException in Macroable.php line 74: Method save does not exist.

this my controller

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();
    }
}
1
  • My guess is that DetailServiceOrder::find doesn't find anything Commented Dec 14, 2016 at 9:11

4 Answers 4

2

You should always check if query returns null. Use is_null or empty():

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();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

hi, i've been following your way but it's still error
1

Maybe try different approach?

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

In this case you don't need to check if record exists because it doesn't use save() method.

For more help check this.

Hope it helps

2 Comments

it's working, but the problem now is the saved data only the last data, foreach seems not working
It should work. Foreach seems good, so i don't get it where is problem with this code?
0

$detailservice is null i think, you should check if it null or not

if ( ! $detailservice) continue;

2 Comments

but when i check using dd($detailservice) the value is not null
-1

I had the same problem, in my case is was just because i use ->get() instead of ->first() like a dummy, obviously there is no method save on a collection... So i reply if someone see this question like me in my research

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.