Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

laravel

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

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:

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

instead I've got:

{"active": false, "person": null, "edit": false, "buttons": false, "text": null}
like image 555
gileneusz Avatar asked Apr 29 '18 16:04

gileneusz


People also ask

How check string is empty in laravel?

The is_null() function checks whether a variable is NULL or not. This function returns true (1) if the variable is NULL, otherwise it returns false/nothing.

IS NOT NULL function in laravel?

Check if not null: whereNotNullSELECT * FROM users WHERE last_name IS NOT NULL; The equivalent to the IS NOT NULL condition in Laravel Eloquent is the whereNotNull method, which allows you to verify if a specific column's value is not NULL .

How do you make a field null in laravel?

Person::saving(function ($person) { // we want to save blank emails as NULL if (empty($person->email)) { $person->email = null; } });


4 Answers

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->text which are falsey and 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.

like image 79
apokryfos Avatar answered Sep 24 '22 00:09

apokryfos


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

<?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
        ];
    }
}
like image 33
kurtkandora Avatar answered Sep 22 '22 00:09

kurtkandora


You can solved with your's database structure ;

$table->string('person')->default('');
like image 29
user6483202 Avatar answered Sep 21 '22 00:09

user6483202


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.

like image 36
Jobayer Avatar answered Sep 21 '22 00:09

Jobayer