Laravel Timestamps更新有关id的错误

时间:2014-07-23 17:19:43

标签: laravel timestamp

您好我是laravel的新手,我现在正在尝试对我的应用程序进行身份验证。 我尝试通过电子邮件检查激活帐户时遇到的问题是:

SQLSTATE [42S22]:未找到列:1054未知列' id'在' where子句' (SQL:update users set updated_at = 2014-07-23 16:51:55,code =,active = 1其中id为空)

在我的数据库中,我有一个名为" idUser"的列。如何更改默认列" id"进入" idUser"?

这是我在控制器中的代码。

/*
Email Activation
*/
public function getActivate($code){
     $user=User::where('code','=',$code)->where('active','=',0);
     if($user->count()){

        $user = $user->first();
        //Update user to active state
        $user->active   =1;
        $user->code     ='';
        if($user->save()){
           return Redirect::route('home')
           ->with('flash_message','Activated! You can now sign in!');
        }

     }
    return Redirect::route('home')
        ->with('flash_message','Could not activate your account.Please try again later. ');
}

3 个答案:

答案 0 :(得分:0)

阅读文档:http://laravel.com/docs/eloquent#basic-usage

// Model
protected $primaryKey = 'whateverColumnYouHave';

答案 1 :(得分:0)

将用户模型中的$ primaryKey设置为idUser:

protected $ primaryKey ='idUser';

注意:Eloquent还会假设每个表都有一个名为id的主键列。您可以定义primaryKey属性来覆盖此约定。同样,您可以定义连接属性以覆盖使用模型时应使用的数据库连接的名称。

来源:http://laravel.com/docs/eloquent#basic-usage

答案 2 :(得分:0)

打开您的模型并添加此受保护变量。这将覆盖现有的。

protected $ primaryKey =' idUser&#39 ;; //这将更改默认主键。

例如:

应用/模型/ user.php的

class User extends Eloquent  {

    protected $table = 'userlists'; //This will change your default table

    protected $primaryKey = 'id'; //This will change the default primary key.

    protected $hidden = array('password'); 

    public function getAuthIdentifier()
    {
        return $this->getKey();
    }

}