将 fname 和 lname 转换为驼峰大小写,同时从模型中保存



>我有员工表,无论用户使用该模型输入如何,我都想在其中将 fname 和 lname 列姆保存为驼峰大小写。我正在使用热心进行验证,还有其他方法吗?我使用了像波纹管这样的突变器,但它仍然保存为普通外壳。我做错了什么?

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

这些突变器是正确的,但是在创建新的表行时需要使用 Eloquent 以使这些突变器正常工作:

$employee = new Employee();
$employee->fname = $request->fname;
$employee->lname = $request->lname;
$employee->save();

确定要转换为camel_case

public function setLnameAttribute($value)
{
    $this->attributes['lname'] = camel_case($value);
}

最新更新