laravel:如何设置我的资源以提供空字符串而不是 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50088688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:41:33  来源:igfitidea点击:

laravel: how to set my resource to give empty string instead of null

laravel

提问by gileneusz

I've got a database with nullable fields. When I send my values via api resource, laravel is sending nullvalues. I want to get empty strings instead. How can I set it up?

我有一个包含可为空字段的数据库。当我通过 发送我的值时api resource,laravel 正在发送null值。我想得到空字符串。我该如何设置?

example:

例子:

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\Resource;

class RequirementResource extends Resource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'active' => $this->active,
            'person' => $this->person, //sometimes has null value
            'edit' => false,
            'buttons' => false,
            'text' => $this->text, //sometimes has null value
        ];
    }
}

I want a json object:

我想要一个 json 对象:

{"active": false, "person": "", "edit": false, "buttons": false, "text": ""}

instead I've got:

相反,我有:

{"active": false, "person": null, "edit": false, "buttons": false, "text": null}

采纳答案by apokryfos

There's a greater question that comes into play here and it's whether your field should be nullable to begin with. Normally you could solve this by not having the field to be nullable which will force you to put an empty string in at insert/update time instead of when displaying it. However I do realise that it's not unreasonable to allow nulls in the database but never return them when returning the resource.

这里有一个更大的问题,它是您的字段是否应该可以为空。通常,您可以通过使字段不为空来解决此问题,这将迫使您在插入/更新时而不是在显示时将空字符串放入。但是我确实意识到在数据库中允许空值但在返回资源时从不返回它们并不是不合理的。

This being said you can solve your problem as follows:

话虽如此,您可以按如下方式解决您的问题:

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\Resource;

class RequirementResource extends Resource
{
    public function toArray($request)
    {
        return [
            'active' => $this->active,
            'person' => $this->person !== null ? $this->person : '',
            'edit' => false,
            'buttons' => false,
            'text' => $this->text !== null ? $this->text : '', 
        ];
    }
}

As Dvek mentioned this can be shortened to $this->text ? : ''but there's a small caveat that $this->text ? : ''will return ''for all values of $this->textwhich are falseyand not necessarily null. In your particular case since text is either a string or null it will be the same but this is not always true.

作为Dvek提到这可以缩短到$this->text ? : '',但有一小告诫说,$this->text ? : ''将返回''的所有值$this->text它们falsey并不见得空。在您的特定情况下,由于 text 是字符串或 null 它将是相同的,但这并不总是正确的。

回答by kurtkandora

if you use php 7 then you should be able to use the double question mark operator:

如果您使用 php 7,那么您应该能够使用双问号运算符:

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\Resource;

class RequirementResource extends Resource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'active' => $this->active,
            'person' => $this->person ?? '', //sometimes has null value
            'edit' => false,
            'buttons' => false,
            'text' => $this->text ?? '', //sometimes has null value
        ];
    }
}

回答by Jobayer

Change your column & Set empty string as default value for that column. Then when you save any column without any value, it will store empty string for it.

更改您的列并将空字符串设置为该列的默认值。然后,当您保存没有任何值的任何列时,它将为其存储空字符串。

回答by user6483202

You can solved with your's database structure ;

你可以用你的数据库结构解决;

$table->string('person')->default('');