2014-09-24 23 views
0

我使用的Laravel 4.2,我試圖授權我自己的模型(我不使用User模型)。 這個問題似乎當通過郵件和密碼,然後我使用的方法Auth::attempt進入到其他(即它對應於錯誤)爲什麼我自己的身份驗證不能在Laravel中使用我自己的模型?

Usuario控制器

class UsuarioController extends BaseController{ 

function doLogin(){ 
    $userdata = array(
     'Correo' => Input::get('correo'), 
     'Contrasena' => Input::get('contrasena') 
     ); 
    if(Auth::attempt($userdata)){ 
     echo 'SUCCESS!'; 

    }else{ 
     echo 'Error!'; 

    } 
} ... 

Usuario模型

use Illuminate\Auth\UserInterface; 
use Illuminate\Auth\Reminders\RemindableInterface; 
class Usuario extends Eloquent implements UserInterface, RemindableInterface { 
protected $table = 'Usuario'; 
protected $primaryKey = 'idUsuario'; 
protected $fillable = array(
         'Nombre', 
         'Apellido', 
         'Rol', 
         'Correo', 
         'Contarsena', 
         'Cumpleanos', 
         'Foto', 
         'Pais', 
         'Comuna', 
         'Profesion_idProfesion', 
         'Institucion_idInstitucion', 
         'remember_token' 
        ); 

function profesion(){ 
    return $this->belongsTo('Profesion', 'idProfesion'); 
} 


public function getPasswordAttribute() 
{ 
    return $this->Contrasena; 
} 

public function setPasswordAttribute($Contrasena) 
{ 
    $this->Contrasena= $Contrasena; 
} 

public function getReminderEmail() 
{ 
    return $this->Correo; 
}  

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

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

public function getRememberTokenName() 
{ 
    return 'remember_token'; 
} 
public function getAuthIdentifier() 
{ 
    return $this->getKey(); 
} 
public function getAuthPassword() { 
    return $this->Contrasena; 
}  

} 

Auth.php

return array(

'driver' => 'eloquent', //database or eloquent 

'model' => 'Usuario', 

'table' => 'Usuario', 

'username' => 'Correo', 
'password' => 'Contrasena', 

'reminder' => array(

    'email' => 'emails.auth.reminder', 

    'table' => 'password_reminders', 

    'expire' => 60, 

), 

); 

Usuario表

enter image description here

應用永不死機,但在如果條件隨時進入到'其他 '' 返回錯誤!

回答

0

您的可填寫陣列一個錯字:

protected $fillable = array(
        'Nombre', 
        'Apellido', 
        'Rol', 
        'Correo', 
        'Contarsena', 
        'Cumpleanos', 
        'Foto', 
        'Pais', 
        'Comuna', 
        'Profesion_idProfesion', 
        'Institucion_idInstitucion', 
        'remember_token' 
       ); 

Contarsena應該Contrasena

而您的驗證數組應包含一個emailpassword鍵:

$userdata = array(
    'correo' => Input::get('correo'), 
    'password' => Input::get('contrasena') 
); 
+0

如果我這樣做返回''未找到列:1054未知列'電子郵件'在'where子句'(SQL:select * from'Usuario'其中'email' = [email protected]限制1)' '所以我用''Correo''替換''email'並且工作:D – cheloncio 2014-09-25 15:16:54

0

嘗試

dd(DB::getQueryLog()); 

得到的SQL執行。這使故障排除更容易。

我的猜測是,有沒有「密碼」字段,嘗試方法會自動哈希

相關問題