简体   繁体   English

在 Laravel 资源中返回空 object 而不是 null

[英]Returning empty object in Laravel resource instead of null

I do have a resource (DeviceResource) in my Laravel API which contains another resource (SimcardResource).我的 Laravel API 中确实有一个资源 (DeviceResource),其中包含另一个资源 (SimcardResource)。 There is a .netoOne relationship between those resources but sometimes a device has no associated simcard.这些资源之间存在 .netoOne 关系,但有时设备没有关联的 SIM 卡。 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 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:我的设备资源 class 如下所示:

    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 class 中定义的字段,所以我尝试了以下操作:

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

But it still returns null instead of an empty json object.但它仍然返回 null 而不是空的 json object。

Okay maybe its not the best solution but my DeviceResource class now looks like this:好吧,也许这不是最好的解决方案,但我的 DeviceResource class 现在看起来像这样:

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 Laravel 介绍最好的方法,例如whenLoaded ,但试试这个

... ?? 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.这是 Laravel 资源的默认行为,如果您没有任何数据,资源也会返回给您 null 资源 object。您必须自己以其他方式管理它,例如通过定义每个参数具有 null 值就是这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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