Reputation:
My Laravel update operation returns true
for dd($data)
. But I want it to return the updated data. How can I change this to get that output? There is one condition I have that the update should be called directly on the model. Any suggestions?
$data = FileLogs::find($id)->update([
'orderId' => $request->orderId,
'fileId' => $request->fileId,
'status' => $request->status
]);
Upvotes: 2
Views: 1006
Reputation: 2453
That's not how it works. You can't expect this query will return you an object:
$data = FileLogs::find($id)->update([
'orderId' => $request->orderId,
'fileId' => $request->fileId,
'status' => $request->status
]);
If you want to use Query Builder only as you mentioned in your question, you'll need to get an object manually:
$data = FileLogs::where('id', $id)->first();
With Eloquent you can use the updateOrCreate()
:
$data = FileLogs::where('id', $id)->updateOrCreate([
'orderId' => $request->orderId,
'fileId' => $request->fileId,
'status' => $request->status
]);
This will return an object. update()
will return boolean, so you can't use it here.
Upvotes: 0
Reputation: 1
First find the data and then you can update it and get it like this
$data = FileLogs::find($id);
Now update like this
$data->update(['orderId'=>$request->orderId,'fileId'=>$request->fileId,'status'=>$request->status]);
Now you can do whatever with the data
dd($data);
Upvotes: 0
Reputation: 1081
First, update the data then fetch it again
$data=FileLogs::find($id)->update(['orderId'=>$request->orderId,'fileId'=>$request->fileId,'status'=>$request->status]);
$updated_data=FileLogs::find($id);
dd($updated_data);
Upvotes: 0
Reputation: 4808
Try this
$data = tap(FileLogs::find($id))
->update(['orderId' => $request->orderId, 'fileId' => $request->fileId, 'status' => $request->status])
->first();
dd($data);
Upvotes: 1