Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);
like image 591
misry Avatar asked Nov 15 '19 06:11

misry


People also ask

What is Eloquent orm in Laravel?

Eloquent is an object relational mapper (ORM) that is included by default within the Laravel framework. An ORM is software that facilitates handling database records by representing data as objects, working as a layer of abstraction on top of the database engine used to store an application's data.

Why use Eloquent in Laravel?

The Eloquent ORM included with Laravel provides a beautiful, simple ActiveRecord implementation for working with your database. Each database table has a corresponding "Model" which is used to interact with that table. Models allow you to query for data in your tables, as well as insert new records into the table.

Can I use eloquent ORM without Laravel?

If you want, you can use Eloquent without Laravel. Actually, Laravel is not a monolithic framework. It is made up of several, separate parts, which are combined together to build something greater.


Video Answer


5 Answers

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!

like image 192
Adam Kozlowski Avatar answered Nov 01 '22 17:11

Adam Kozlowski


You can try this one too.

 $results = ClientVendor::where('client_id','=', $request->client_id)
                             ->where('vendor_id','=',$request->vendor_id)
                             ->update([
                                     'status' => $request->status
                               ]);
like image 26
Prashant Deshmukh..... Avatar answered Nov 01 '22 18:11

Prashant Deshmukh.....


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);
like image 23
Frncs Avatar answered Nov 01 '22 17:11

Frncs


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);
like image 25
I Gusti Ngurah Arya Bawanta Avatar answered Nov 01 '22 17:11

I Gusti Ngurah Arya Bawanta


Many have suggested using first() instead of get(). I think fistOrFail() would be a better option to handle null results.

Otherwise, in cases where the result is null, you'd get Call to a member function save() on null error.

Eg:

ClientVendor does not have a record with either client_id or vendor_id matching $request->client_id or $request->client_id respectively.

$result = ClientVendor::where('client_id', $request->client_id)
                         ->where('vendor_id',$request->vendor_id)
                         ->first(); 
$result->status = $request->status; //produces error Creating default object from empty value
$result->save(); //produces error "Call to a member function save() on null" because $result will be empty

The above code produces 2 exceptions: 1. "Creating default object from empty value" on lines $result->sataus = .. and 2."Call to a member function save() on null" on line $result->save because $result will be empty.

To fix that, you would call firstOrFail() like so.

$result = ClientVendor::where('client_id', $request->client_id)
                             ->where('vendor_id',$request->vendor_id)
                             ->firstOrFail();
$result->status = $request->status; 
$result->save();

This fix, firstOrFail, when there are no results would produce a 404, a handled error, and the subsequent lines would not be executed.

For situations where you query using find($id), it is better to use findOrFail($id)

like image 41
user3532758 Avatar answered Nov 01 '22 18:11

user3532758