How to change empty string to a null using Laravel 5.1?

ぃ、小莉子 提交于 2019-12-10 18:41:25

问题


While using Laravel 5.1, I am trying to check every value before it is saved in the database using Eloquent ORM. My logic is, first trim the value, if the value is an empty string "", then to convert it to null instead of just an empty string.

I was advised to create a Trait which will override the setAttribute method for that.

So here is what I have done

I have a new folder "app\Traits" inside of a file called TrimScalarValues.php which contains the following code

<?php

namespace App\Traits;

trait TrimScalarValues
{
    public function setAttribute($key, $value)
    {
        if (is_scalar($value)) {
            $value = $this->emptyStringToNull(trim($value));
        }

        return $this->setAttribute($key, $value);
    }


    /**
     * return null value if the string is empty otherwise it returns what every the value is
     *
    */
    private function emptyStringToNull($string)
    {
        //trim every value
        $string = trim($string);

        if ($string === ''){
           return null;
        }

        return $string;
    }
}

Finally I have a app\Models\Account.php file which contains the following code

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Models\industry;
use App\Traits\RecordSignature;
use App\Traits\TrimScalarValues;


class Account extends Model
{
    use RecordSignature, TrimScalarValues;
    /**
     * The database table used by the model.
     *
     * @var string
    */
    protected $table = 'accounts';

    protected $primaryKey = 'account_id';

    const CREATED_AT = 'created_on';

    const UPDATED_AT = 'modified_on';

    const REMOVED_AT = 'purged_on';


    /**
     * The attributes that are mass assignable.
     *
     * @var array
    */
    protected $fillable = ['client_id','account_name', 'company_code', 'legal_name', 'created_by','modified_by','instrucations'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
    */
    //protected $hidden = ['account_id', 'remember_token'];


    protected $guarded = ['account_id'];

    /**
     * Get the industry record associated with the account.
    */
    public function industry()
    {
        return $this->hasOne(industry, industry::primaryKey);
    }

    public function pk(){

        return $this->primaryKey;
    }

}

But every time I update a value, I get a white page with no error or logs.

When I modify the app\Models\Account.php and change use RecordSignature, TrimScalarValues; to use RecordSignature; then I do not get a white page but obviously the values are not trimmed and converted to null.

What am I doing wrong here?


回答1:


You can't call $this->setAttribute() in your trait. Instead you want to call the "original" setAttribute method by using parent:::

public function setAttribute($key, $value)
{
    if (is_scalar($value)) {
        $value = $this->emptyStringToNull(trim($value));
    }

    return parent::setAttribute($key, $value);
}

Regarding the empty logs, have you checked the webserver log besides the one from the framework?




回答2:


I had the same problem and solved it by creating a middleware that filters empty input fields.

public function handle($request, Closure $next) {
    $input = $request->all();
    if ($input) {
        array_walk_recursive($input, function (&$item) {
            $item = trim($item);
            $item = ($item == "") ? null : $item;
        });
        $request->merge($input);
    }
    return $next($request);
}

Don't forget to add your custom middleware to Http/Kernel.php

Found this at Laracasts




回答3:


You might wanna take a look at this package: https://packagist.org/packages/iatstuti/laravel-nullable-fields

"This create a trait and allows you to easily flag attributes that should be set as null when being persisted to the database."

I know this post is old, and at that time this package maybe didn't exist yet.




回答4:


You can use mutator in you model. For the field account_name mutator should looks like this:

public function setAccountNameAttribute($account_name)
{
    if(is_null($account_name))
    {
        $this->attributes['account_name'] = null;
    }
    else
    {
        $this->attributes['account_name'] = $account_name;
    }
}

And everytime when you will update or insert the record using Eloquent, account_name will be passed through this mutator.



来源:https://stackoverflow.com/questions/31708707/how-to-change-empty-string-to-a-null-using-laravel-5-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!