如何使用Laravel 5.1将空字符串更改为null?

Jun*_*ior 3 php traits laravel laravel-5.1

使用Laravel 5.1时,我试图在使用Eloquent ORM将数据保存到数据库之前检查每个值.我的逻辑是,首先修剪该值,如果该值是空字符串"",则将其转换为null而不是仅仅为空字符串.

我被建议创建一个Trait,它将覆盖setAttribute方法.

所以这就是我所做的

我在一个文件里面有一个新的文件夹"app\Traits",里面 TrimScalarValues.php包含以下代码

<?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;
    }
}
Run Code Online (Sandbox Code Playgroud)

最后我有一个app\Models\Account.php包含以下代码的文件

<?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;
    }

}
Run Code Online (Sandbox Code Playgroud)

但每次我更新一个值,我都会得到一个没有错误或日志的白页.

当我修改app\Models\Account.php并更改use RecordSignature, TrimScalarValues;use RecordSignature;然后我没有得到白页但显然这些值没有被修剪并转换为null.

我在这做错了什么?

luk*_*ter 5

你不能打电话给$this->setAttribute()你的特质.相反,您想setAttribute通过使用parent::以下方法调用"原始" 方法:

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

    return parent::setAttribute($key, $value);
}
Run Code Online (Sandbox Code Playgroud)

关于空日志,您是否已检查过框架中的Web服务器日志?


小智 5

我遇到了同样的问题,并通过创建一个过滤空输入字段的中间件来解决它.

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);
}
Run Code Online (Sandbox Code Playgroud)

不要忘记将自定义中间件添加到Http/Kernel.php

Laracasts找到了这个