我有employee表,我希望将fname和lname colomns保存为camel case,无论使用该模型的用户输入如何。我正在使用热心的验证是否还有其他方法吗?我使用像波纹管一样的变形器,但仍然像普通套管那样保存。我做错了什么?
protected $fillable = [
'fname','lname'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $table = 'employees';
protected $guarded = array('id', 'password');
public static $rules = array(
'fname' => 'required',
'lname' => 'required'
);
/**
* Make sure that first name is capitalized BEFORE saving it to the database
*
* @param $value
* @return string
*/
public function setFnameAttribute($value)
{
$this->attributes['fname'] = ucfirst($value);
}
/**
* Make sure that last name is capitalized BEFORE saving it to the database
*
* @param $value
* @return string
*/
public function setLnameAttribute($value)
{
$this->attributes['lname'] = ucfirst($value);
}
答案 0 :(得分:0)
这些mutator是正确的,但是在创建新的表行时需要使用Eloquent来使这些mutator工作:
$employee = new Employee();
$employee->fname = $request->fname;
$employee->lname = $request->lname;
$employee->save();
答案 1 :(得分:0)
您确定要转换为camel_case
吗?public function setLnameAttribute($value)
{
$this->attributes['lname'] = camel_case($value);
}