Skip to content
Advertisement

Laravel API Resourse returning relation only on ‘collection’ method

guys I’m new to Laravel. I currently building an API, and I have a couple of endpoints (the one that we will discuss are the Get All Tasks and Get Single Task)

The problem is that whenever I call the Get All Tasks endpoint it returns me the task resource + the user resource there. However when I call the Get Single Task endpoint it only returns me the TaskResource without the User inside of it. Any idea ?

Here is the code for the resourses

JavaScript

And here is how I get the Tasks in the controller. https://prnt.sc/16q0rlh

Advertisement

Answer

The reason was that I was missing the ‘with(‘user’)’ before the ‘findOrFail’ in my controller so ->

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement