Error Method Illuminate\\Database\\Eloquent\\Collection::save does not exist. in Laravel

20,839

Solution 1

You cant do this because you call whole collection where is many elements. Call just single record, then you can update it.

When you use get() you call collection When you use first() or find($id) then you get single record that you can update.

Look at example:

$results = ClientVendor::where('client_id', $request->client_id)
                        ->where('vendor_id',$request->vendor_id)
                        ->first(); // this point is the most important to change
$results->status = $request->status;
$results->save();
return response()->json($results);;

Good luck!

Solution 2

You can try this one too.

 $results = ClientVendor::where('client_id','=', $request->client_id)
                             ->where('vendor_id','=',$request->vendor_id)
                             ->update([
                                     'status' => $request->status
                               ]);

Solution 3

Try this:

$results = ClientVendor::where('client_id', $request->client_id)
                                 ->where('vendor_id',$request->vendor_id)
        ->first();
        $results->status = $request->status;
        $results->save();
        return response()->json($results);

Solution 4

It depends on your needs, if you want to :

  1. Get and update one record, you should use first() or firstOrFail() instead of get(). Should be look like this :
$results = ClientVendor::where('client_id', $request->client_id)
                         ->where('vendor_id',$request->vendor_id)
                         ->first();
$results->status = $request->status;
$results->save();
return response()->json($results);
  1. Get and update multiple records, yes you can use get(), but you should do foreach and then update the single record one by one. just like this :
$results = ClientVendor::where('client_id', $request->client_id)
                         ->where('vendor_id',$request->vendor_id)
                         ->get();
foreach($results as $result){
    $result->status = $request->status;
    $result->save();
}
return response()->json($results);
Share:
20,839
misry
Author by

misry

Updated on July 31, 2022

Comments

  • misry
    misry almost 2 years

    I want to change the status at $ result, to get the data at $ result I use query builder, but there is an error like that

    $results = ClientVendor::where('client_id','=', $request->client_id)
                                     ->where('vendor_id','=',$request->vendor_id)
            ->get();
            $results->status = $request->status;
            $results->save();
            return response()->json($results);