framework: [API Resource] Error 500 when query result is empty
- Laravel Version: 5.5.22
- PHP Version: 7.1
- Database Driver & Version: MySQL 5.6
Description:
Api resource error when query result is null as follow in official document example.
public function show($id)
{
return new UserResource(User::find($id));
}
Assume that $id = 20 and there is no user with id = 20
Steps To Reproduce:
- return api resource with empty result in controller
- 500 server error occur
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 15 (7 by maintainers)
But you will still get error if return a transformation array instead of
return parent::toArray($request);:The responsibility of a resource class is to transform your model into JSON. That obviously isn’t gonna work when you pass null. You better check for existence of the model in your controller, or use
findOrFailto automatically trigger a 404.But this isn’t the place for code level support, this is for reporting bugs and issues of the framework.
@GuidoHendriks OK, then the official document should not use something like
::find($id). The doc made me think that it doesn’t require to check fornullbefore using it, the expectation is something likenullvalue indatakey.I just overrided the Resource@resolve() method, to handle these kinds of cases by adding this to the start of the method.