简体   繁体   中英

Returning empty object in Laravel resource instead of null

I do have a resource (DeviceResource) in my Laravel API which contains another resource (SimcardResource). There is a .netoOne relationship between those resources but sometimes a device has no associated simcard. If this is the case my DeviceResource returns for the simcard null instead of an empty json object. I do need an empty json object because I present information called from my API in my Vue frontend by accessing an object eg like device.simcard.phone_number

My DeviceResource class looks like this:

    public function toArray($request)
    {
        return [
            'id' => $this->resource->id,
            'model' => $this->resource->model,
            'device_name' => $this->resource->device_name,
            'operating_system' => $this->resource->operating_system,
            'os_version' => $this->resource->os_version,
            'emei' => $this->resource->emei,
            'device_password' => $this->resource->device_password,
            'purchase_date' => $this->resource->purchase_date,
            'associated_worker' => $this->resource->associated_worker,
            'remarks' => $this->resource->remarks,
            'device_status_id' => $this->resource->device_status_id,
            // 'simcard' => $this->resource->simcard ?: (object)[],
            'simcard' => SimcardResource::make($this->whenLoaded('simcard'))  ?: (object)[],
        ];
    }

The commented section:

'simcard' => $this->resource->simcard ?: (object)[]

Works perfectly but returns all fields from my simcard table but I only need fields defined in my SimcardResource class so I tried the following:

'simcard' => SimcardResource::make($this->whenLoaded('simcard'))  ?: (object)[]

But it still returns null instead of an empty json object.

Okay maybe its not the best solution but my DeviceResource class now looks like this:

public function toArray($request)
{
    if (is_null($this->resource->simcard)) {
        return [
            'id' => $this->resource->id,
            'model' => $this->resource->model,
            'device_name' => $this->resource->device_name,
            'operating_system' => $this->resource->operating_system,
            'os_version' => $this->resource->os_version,
            'emei' => $this->resource->emei,
            'device_password' => $this->resource->device_password,
            'purchase_date' => $this->resource->purchase_date,
            'associated_worker' => $this->resource->associated_worker,
            'remarks' => $this->resource->remarks,
            'device_status_id' => $this->resource->device_status_id,
            'simcard' => (object) [],
        ];
    } else {
        return [
            'id' => $this->resource->id,
            'model' => $this->resource->model,
            'device_name' => $this->resource->device_name,
            'operating_system' => $this->resource->operating_system,
            'os_version' => $this->resource->os_version,
            'emei' => $this->resource->emei,
            'device_password' => $this->resource->device_password,
            'purchase_date' => $this->resource->purchase_date,
            'associated_worker' => $this->resource->associated_worker,
            'remarks' => $this->resource->remarks,
            'device_status_id' => $this->resource->device_status_id,
            // 'simcard' => $this->resource->simcard ?: (object)[],
            'simcard' => SimcardResource::make($this->whenLoaded('simcard')),
        ];
    }
}

Laravel introduce best ways,for example whenLoaded ,but try this

... ?? json_encode(new stdClass)

It is Laravel resource default behaviour that if you do not have any data than resource will also return you the null resource object. You have to manage it yourself in other way like by defining each parameter has null value that's it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM