0

In my DoctorController, I have show method like below -

public function show(Doctor $doctor){
   DoctorResource::withoutWrapping();
   return new DoctorResource($doctor->load('expertises'));
}

In my DoctorResource, if I do

public function toArray($request){
    return parent::toArray($request);
}

It returns -

{
    "id": 1,
    "name": "John Doe",
    "mobile": "1234567890",
    "email": null,
    "avatar": null,
    "bio": "Lorem Ipsum 2",
    "email_verified_at": null,
    "mobile_verified_at": null,
    "is_account_verified": 0,
    "account_status": "Active",
    "created_at": "2020-12-03T07:45:07.000000Z",
    "updated_at": "2020-12-03T10:49:30.000000Z",
    "expertises": [
        {
            "id": 1,
            "expertise": "ABC",
            "created_at": null,
            "updated_at": null,
            "pivot": {
                "doctor_id": 1,
                "expertise_id": 1,
                "created_at": "2020-12-03T10:49:29.000000Z",
                "updated_at": "2020-12-03T10:49:29.000000Z"
            }
        },
        {
            "id": 2,
            "expertise": "XYZ",
            "created_at": null,
            "updated_at": null,
            "pivot": {
                "doctor_id": 1,
                "expertise_id": 2,
                "created_at": "2020-12-03T10:49:29.000000Z",
                "updated_at": "2020-12-03T10:49:29.000000Z"
            }
        }
    ]
}

But I want to return certain fields from my DoctorResource, So I did this but it is giving me an error. Exception: Property [expertise] does not exist on this collection instance.

   public function toArray($request){
        return [
            'id' => $this->id,
            'name' => $this->name,
            'mobile' => $this->mobile,
            'email' => $this->email,
            'avatar' => $this->avatar,
            'bio' => $this->bio,

            'expertises' => [
                'name' => $this->expertises->expertise
            ]
        ];
    }
1
  • $this->expertises is a Collection, so you can't access expertise. You could do $this->expertises->first()->expertise to get 'ABC', or $this->expertises->pluck('expertise') to get an array ['ABC', 'XYZ']. But regardless, the way you're trying to do it now is invalid. Commented Dec 8, 2020 at 16:44

1 Answer 1

1

you have define another Json Resource to do that:

first you can retrive the model and add keys and values based on each model properties like :

public function toArray($request){
        return [
            'id' => $this->id,
            'name' => $this->name,
            'mobile' => $this->mobile,
            'email' => $this->email,
            'avatar' => $this->avatar,
            'bio' => $this->bio,

            'expertises' => ExpertiesResource::collection($this->experties);
        ];
    }

and you have to define Resource Collection as Follow:

class ExpertiesResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
          "id" => $this->id,
          "exp" => $this->exp,
          .....
        ];
    }
}

when your JsonResource has an Array of Object(Resource), you have to Define That Resource and make use of it like use App\Http\Resources\ExpertiesResource

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

7 Comments

i am editing and will be availible in couple of minutes
Answers are saved as a draft as you type them, don't need to post your answer until it is complete :) (But thanks for posting a comment; should stave off the downvotes in the meantime)
Ok, I got it. So, if I need a separate expertise resource which will return all the fields from the expertise model, then I have to make 2 expertise resource. Right?
you have to write another one. its better to write a Definition Resource Class for Each Type of Data Your App Returning. I have an App with Model name Entity but for multiple scenario i want different aspect of it. so i have multiple JsonResource such as EntityDistributor, EntitySaleable, EntityCompleteResource, ...
also you could use 'expertises' => $this->experties but you couldnt instruct which inner attribute you want in output
|

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.