user() ? Auth()->user()->type : RoleTypes::USER; $exceptionUsers = in_array($userType, [RoleTypes::SHADOW_ADMIN, RoleTypes::SUPER_ADMIN]); //The Base $data = [ 'id' => $this->id, 'claimant_id' => $this->claimant_id, 'reference' => $this->reference, 'status' => $this->status, 'transport' => new TransportResource($this->transports()->first()), 'type' => $this->type, 'container' => new ContainerResource($this->containers->first()), $this->mergeWhen($this->owner instanceof Order, [ 'order' => New OrderResource($this->owner) ]), ]; //option 1: include_packages $packages = !$this->packingLists()->exists() || $exceptionUsers ? $this->packages : $this->packingLists->first()->packages; $data['packages'] = PackageBaseResource::collection($packages); return $data; } }