Laravel'User'模型编号ID列 [英] Laravel 'User' model no id column

查看:168
本文介绍了Laravel'User'模型编号ID列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用User模型进行Auth :: attempt()时出现错误,说我的表中没有'id'列.我将用户名"字段用作表中的主键,但我不想将int的"id"列用作主键.无论如何,我可以更改用户模型中的主键列吗?

I'm getting an error when trying to Auth::attempt() with the User model saying that I do not have a 'id' column in my table. I am using the 'username' field as the primary key in my table and I didn't want to make an int 'id' column as the primary key. Is there anyway I can change the primary key column in the User model?

错误:

我的用户模型:

    <?php

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait;

    protected $table = 'users';

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


        public function getRememberToken()
        {
            return $this->remember_token;
        }   

        public function setRememberToken($remember_token) 
        {
            $this->remember_token   =   $remember_token;
        }

        public function getRememberTokenName()
        {
            return 'remember_token';
        }

        public function isAdmin()
        {
            return $this->isAdmin   ==  1;
        }

}

推荐答案

要覆盖用于搜索的主键,必须在用户模型上定义primaryKey属性.

To override the primary key used to search you must define the primaryKey property on the User model.

class User extends Eloquent implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait;

    protected $primaryKey = 'username';
...

现在laravel将使用用户名列作为主键列.

Now laravel will use the username column as the primary key column.

请参阅laravel文档中的以下引文.

See the below quote from the laravel documentation.

注意:口才也将假设每个表都有一个主键 列名为id.您可以定义primaryKey属性以覆盖此属性 惯例.

Note: Eloquent will also assume that each table has a primary key column named id. You may define a primaryKey property to override this convention.

-口才-基本用法

这篇关于Laravel'User'模型编号ID列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆