donglinxia1541 2014-07-28 04:54 采纳率: 0%
浏览 27
已采纳

Eloquent模型不向数据库添加列

This is a simple problem. When I call save() on my model, the columns are not getting added to the database. Here is my model:

class User extends Eloquent implements UserInterface, RemindableInterface {
    use UserTrait, RemindableTrait;

    protected $table = 'users';
    protected $hidden = array('password', 'remember_token');

    public $email; ## string (used as username)
    public $name; ## string
    public $address; ## string
    public $phone; ## string nullable
}

and here's the code to add the row

$data=Input::all();

if ($data['password'] != $data['confirm-password']){
    return Redirect::to('/register');
}

$user = new User;

$user->email=$data['email'];
$user->password=Hash::make($data['password']);
$user->name=$data['first']." ".$data['last'];
$user->address=$data['street'].", ".$data['city'].", ".$data['state']." ".$data['zip'];
$user->phone=$data['phone'];

$user->save();

echo "<pre>";
var_dump($user);
echo "</pre>";

The var_dump is outputting the proper values in the model. Every field in the Model matches exactly the name of the column in the database. The password hash is getting added and a new row is being created, but every other column is showing up empty. What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • dongping4273 2014-07-28 04:56
    关注

    You can't to declare columns as object properties. Laravel will do the magic for you. Remove these lines:

    public $email; ## string (used as username)
    public $name; ## string
    public $address; ## string
    public $phone; ## string nullable
    

    Explanation:

    Laravel's Eloquent make use of PHP's __set() (reference) which is triggered when you are setting to an undefined property. Laravel then performs a few internal tasks via Model::setAttribute() to populate the model in its own way.

    See Illuminate/Database/Eloquent/Model.php:

    public function __set($key, $value)
    {
        $this->setAttribute($key, $value);
    }
    
    public function setAttribute($key, $value)
    {
        // First we will check for the presence of a mutator for the set operation
        // which simply lets the developers tweak the attribute as it is set on
        // the model, such as "json_encoding" an listing of data for storage.
        if ($this->hasSetMutator($key))
        {
            $method = 'set'.studly_case($key).'Attribute';
    
            return $this->{$method}($value);
        }
    
        // If an attribute is listed as a "date", we'll convert it from a DateTime
        // instance into a form proper for storage on the database tables using
        // the connection grammar's date format. We will auto set the values.
        elseif (in_array($key, $this->getDates()))
        {
            if ($value)
            {
                $value = $this->fromDateTime($value);
            }
        }
    
        $this->attributes[$key] = $value;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?