2017-09-02 174 views
0

我試着去創造登錄表單及其控制器,但是當我嘗試登錄它不工作任何一個可以幫助我,我在laravel很新。Laravel 5.4認證

這裏是我的形式

<form action="/login" method="post"> 
     {{ csrf_field() }} 

     <div class="form-group has-feedback"> 
      <input type="email" name="email" class="form-control" placeholder="Email"> 
      <span class="glyphicon glyphicon-envelope form-control-feedback"></span> 
     </div> 
     <div class="form-group has-feedback"> 
      <input type="password" name="password" class="form-control" placeholder="Password"> 
      <span class="glyphicon glyphicon-lock form-control-feedback"></span> 
     </div> 
     <div class="row"> 
      <div class="col-xs-7"> 
       <div class="checkbox"> 
        <label> 
         <input type="checkbox"> Remember Me 
        </label> 
       </div> 
      </div> 
      <!-- /.col --> 
      <div class="col-xs-5"> 
       <button type="submit" class="btn btn-primary btn-raised btn-block ">Sign In</button> 
      </div> 
      <!-- /.col --> 
     </div> 
    </form> 

,這裏是我的路線

Route::get('/login', '[email protected]'); 
Route::post('/login', '[email protected]'); 

和我的LoginController是

class loginController extends Controller 
{ 
    public function __construct(){ 
     $this->middleware('guest', ['except' => 'destroy']); 
    } 

    public function create(){ 

     return view('pages.admin.login'); 
    } 


    public function store(){ 

     if(! auth()->attempt(request(['email', 'password']))){ 
      return back()->withErrors([ 
       'message' => 'Please check your credentials' 
      ]); 
     } 

     return redirect('/home'); 
    } 


} 

我的用戶模式是

class User extends Authenticatable 
{ 
    use Notifiable; 

    /** 
    * The attributes that are mass assignable. 
    * 
    * @var array 
    */ 
    protected $fillable = [ 
     'fname','oname','lname', 'email', 'phone','password', 
    ]; 

    /** 
    * The attributes that should be hidden for arrays. 
    * 
    * @var array 
    */ 
    protected $hidden = [ 
     'password', 'remember_token', 
    ]; 
} 

請在那裏我我對這些,你發送請求的返回值做錯了,當我輸入的電子郵件和密碼,我的憑據只是刷新並返回到登錄頁面

+0

請添加錯誤消息 –

+0

它不會引發錯誤 – ProtanzaPlus

回答

0

這是你的企圖功能應該是怎麼樣的:

if(Auth::attempt(['email'=> $request['email'],'password' => $request['password'] ]) 
+0

SQLSTATE [42S22]:列未找到:1054未知列在「例如@ gmail的「where子句」(SQL:從'users'選擇*其中'例如@ gmail' =和'bxndbnbsd'爲空限制1) – ProtanzaPlus

+0

它給我的錯誤,即使我有電子郵件地址和密碼filds – ProtanzaPlus

+0

明確查詢不正確,請注意SQL查詢其說法,其中例如@ gmail的=代替電子郵件= .... – Sletheren

0

你的功能應該是這樣用Request門面。

public function store(Request $request){ 

    if(! auth()->attempt($request->only(['email', 'password']))){ 
     return back()->withErrors([ 
      'message' => 'Please check your credentials' 
     ]); 
    } 

    return redirect('/home'); 
    } 
+0

Stil它做同樣的,我改變和修改它,但仍然是相同的,它不工作 – ProtanzaPlus

0

頂部添加這些的LoginController

use Illuminate\Http\Request; 
use Illuminate\Support\Facades\Auth; 

和修改store方法

public function store(Request $request){ 

    $email = $request->email; 
    $password = $request->password; 

    if (! Auth::attempt(['email' => $email, 'password' => $password])) { 
     // Authentication Failed... 
     return back()->withErrors([ 
      'message' => 'Please check your credentials' 
     ]); 
    } 

    return redirect('/home'); 
} 

同時刪除從$hiddenpasswordUser模型。

/** 
* The attributes that should be hidden for arrays. 
* 
* @var array 
*/ 
protected $hidden = [ 
    'remember_token', 
]; 

希望它有幫助。

0

您可以使用PHP的工匠製作:AUTH和laravel會產生需要包括控制器登錄的一切,那麼你就可以進入你的資源和編輯,使它看起來你如何想象。

這就是我的登錄控制器看起來像使用PHP工匠化妝後:AUTH

<?php 

namespace App\Http\Controllers\Auth; 

use App\Http\Controllers\Controller; 
use Illuminate\Foundation\Auth\AuthenticatesUsers; 

class LoginController extends Controller 
{ 


    use AuthenticatesUsers; 

    /** 
    * Where to redirect users after login. 
    * 
    * @var string 
    */ 
    protected $redirectTo = '/home'; 

    /** 
    * Create a new controller instance. 
    * 
    * @return void 
    */ 
    public function __construct() 
    { 
     $this->middleware('guest')->except('logout'); 
    } 
}