问题描述
在使用Laravel 5.1的同时,我试图在使用Eloquent ORM将每个值保存在数据库中之前检查每个值.我的逻辑是,首先修剪值,如果值是空字符串""
,然后将其转换为null
而不是仅是空字符串.
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.
建议我创建一个Trait,该特性将覆盖setAttribute方法.
I was advised to create a Trait which will override the setAttribute method for that.
这就是我所做的
我在名为TrimScalarValues.php
的文件中有一个新文件夹"app \ Traits",其中包含以下代码
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;
}
}
最后我有一个app\Models\Account.php
文件,其中包含以下代码
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.
当我修改app\Models\Account.php
并将use RecordSignature, TrimScalarValues;
更改为use RecordSignature;
时,我没有得到白页,但是显然这些值没有被修剪并转换为null.
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.
我在这里做什么错了?
推荐答案
您无法在特征中调用$this->setAttribute()
.相反,您想使用parent::
来调用原始" setAttribute
方法:
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);
}
关于空日志,除了框架中的日志以外,您是否还检查过Web服务器日志?
Regarding the empty logs, have you checked the webserver log besides the one from the framework?
这篇关于如何使用Laravel 5.1将空字符串更改为null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!