将Laravel auth与驱动程序数据库一起使用

时间:2015-08-01 11:18:27

标签: php laravel authentication

我想在Laravel 4.2中使用名为auth的自定义表格中的pseudo函数,但我收到此错误:

  

未定义的索引:id

表格的PK是: id_pseudo

这是我的auth.php配置:

'driver' => 'database',
'table' => 'pseudo',

这是表格的模型"伪" :

class pseudo extends Eloquent implements UserInterface, RemindableInterface {

    protected $table = 'pseudo';
    protected $primaryKey = 'id_pseudo';

    /**
     * Get the unique identifier for the user.
     *
     * @return mixed
     */
    public function getAuthIdentifier()
    {

        return $this->id_pseudo;
    }

    /**
     * Get the password for the user.
     *
     * @return string
     */
    public function getAuthPassword()
    {
        return $this->password;
    }

这是我的php代码:

public function authentification()
    {
        $email = Input::get('email');
        $mdp = Input::get('password');

        if (Auth::attempt(array('email' => Input::get('email'), 'password' => Input::get('password'), 'code_court_etat' => 'VALIDE'))){

            exit("signature OK");

        } else {
            exit("signature KO");
        }

我确信问题出现在"模型"文件,但我不知道到底是什么,我是laravel的初学者。

1 个答案:

答案 0 :(得分:1)

您的auth驱动程序应该是eloquent,因为您尝试使用雄辩的模型,而不是直接查询数据库。

app/config/auth.php中,您还应将模型选项设置为模型的类,在本例中为pseudo

有关使用4.2 http://laravel.com/docs/4.2/security

的身份验证的文档中的更多内容

根据评论进行编辑:

由于您当前的模型同时实现了UserInterfaceRemindableInterface,因此您需要为要使用的auth功能的若干功能定义几种方法。

您可以将以下两个特征添加到模型中,以自动添加所需的功能。这假设您的桌子上有以下列:emailpasswordremember_token

use \Illuminate\Auth\Reminders\RemindableTrait, \Illuminate\Auth\Reminders\RemindableInterface;

或者,如果表使用不同的列名,您可以自己实现这些方法。请参阅下面的2个链接,了解需要添加哪些方法以及应该做什么。

https://github.com/laravel/framework/blob/4.2/src/Illuminate/Auth/UserTrait.php https://github.com/laravel/framework/blob/4.2/src/Illuminate/Auth/Reminders/RemindableTrait.php