0

I think I might have found a bug in Laravel, please see the below code:

I am hitting an endpoint which is /api/users/1

public function show(User $user)
{
    return new UserResource($user);
}

My Resource looks like the below:

use Illuminate\Http\Resources\Json\JsonResource;

class LenderTrancheCommitment extends JsonResource
{
    public function toArray($request)
    {

        //dd($this);

        return [
            'uuid'               => $this->uuid,
            'name'               => $this->name,
            'currency'           => $this->currency,
            'company'            => new CompanyResource($this->whenLoaded('company')),
            'created_at'         => $this->created_at,
            'updated_at'         => $this->updated_at,
        ];
    }
}

Now I would expect the company property not to return in the JSON however it does, can anyone see a reason why the company relation is being loaded and added to the JSON response when I am not using ->with() anywhere in my code?

Sorry if this is a total n00b mistake but I want to properly understand the new API resource stuff in Laravel and I feel like I am making a mistake.

2
  • do you have a $with property in model ? try User::setEagerLoads([]) to be sure Commented May 16, 2019 at 23:15
  • also try it like this CompanyResource::collection($this->whenLoaded('company')) Commented May 16, 2019 at 23:27

1 Answer 1

1

I managed to solve this, the issue is due to having an attribute in the model that used property from a relation.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.