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);
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.
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.
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.
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!
You can try this one too.
$results = ClientVendor::where('client_id','=', $request->client_id)
->where('vendor_id','=',$request->vendor_id)
->update([
'status' => $request->status
]);
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);
It depends on your needs, if you want to :
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);
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);
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With