dimanche 18 novembre 2018

How to use conditional relationship in API Resource?

I have created an API Resource:

class Order extends JsonResource
{

    public function toArray($request)
    {
        return [
            "id" => $this->Id,
            "photo" => ''
        ];
    }
}

In controller I use this resource:

public function show($id)
{
        $order = OrderModel::with('OrderPhoto')->findOrFail(1);

        return new OrderResource($order);
}

So, I try to use relation OrderPhoto in API Resource like this:

public function toArray($request)
    {
        return [
            "id" => $this->Id,
            "photo" => OrderPhotoResource::collection($this->whenLoaded('OrderPhoto')),
        ];
    }

But it does not work



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire