Laravel Resource返回空值:更新接口无响应但数据库已更新
Hey there, let's troubleshoot this frustrating issue you're dealing with. It's super odd that your store method returns the CallResource as expected, but the update returns a blank response even though the database gets updated correctly. Let's break down the most likely fixes step by step:
1. The #1 Culprit: You're not passing a valid Model instance to CallResource
This is the most common mistake with this scenario. Let's look at two possible bad patterns:
Case A: Using query builder update() which returns a count, not a model
If your update method looks like this:
public function update(Request $request) { $validated = $this->validate($request, [ 'title' => 'required', 'job_id' => 'required', // other rules ]); // This returns the number of rows affected, NOT a Call model instance $call = Call::where('id', $request->id)->update($validated); return new CallResource($call); // 🚨 Passing a number, not a model }
Call::where()->update() returns an integer (count of updated rows), not a model. When you pass that to CallResource, it can't resolve any model attributes, resulting in a blank response.
Fix: Re-fetch the model after updating:
public function update(Request $request) { $validated = $this->validate($request, [ 'title' => 'required', 'job_id' => 'required', // other rules ]); Call::where('id', $request->id)->update($validated); // Fetch the freshly updated model $call = Call::findOrFail($request->id); return new CallResource($call); }
Case B: Forgetting to refresh the model after update (less common, but possible)
If you're using route model binding (injecting the Call model directly), $call->update() should update the model's attributes automatically—but in rare cases (like if you have observers or custom logic overriding this), the model might hold stale data. Add refresh() to force it to pull the latest from the database:
public function update(Request $request, Call $call) { $validated = $this->validate($request, [ 'title' => 'required', 'job_id' => 'required', // other rules ]); $call->update($validated); // Refresh to get the latest DB state $call->refresh(); return new CallResource($call); }
2. Check your CallResource for conditional field filtering
If your resource uses conditional fields (like $this->when() or $this->mergeWhen()), it's possible that all fields are being filtered out for update requests. For example:
class CallResource extends JsonResource { public function toArray($request) { return [ 'id' => $this->id, 'title' => $this->when($request->isMethod('POST'), $this->title), // 🚨 Only returns title on store // other conditional fields ]; } }
Double-check your resource's toArray() method to ensure fields aren't restricted to only POST (store) requests.
3. Quick Debugging Trick
To narrow down the issue, temporarily replace the CallResource return with a raw model dump:
// Replace return new CallResource($call); with: return response()->json($call->toArray());
If this returns data, the problem is in your CallResource class. If it still returns blank, the issue is that $call isn't a valid, populated model instance.
内容的提问来源于stack exchange,提问作者CircularRecursion

