2016-11-01 73 views
1

我在Laravel 5.3上,我正在使用視圖,我打算共享一個全局變量到我的所有視圖,並且這個全局變量包含誰是當前登錄用戶的信息HomeController.php中的ErrorException錯誤27:嘗試獲取非對象的屬性

<?php 

namespace App\Http\Controllers; 

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

//use models 
use App\profile; 

class HomeController extends Controller 
{ 
    /** 
    * Create a new controller instance. 
    * 
    * @return void 
    */ 

    public function __construct() 
    { 
     $this->middleware('auth'); 

     View::share('user_info',profile::where('username',Auth::user()->username)->get()); 

    } 
    public function index() 
    { 
     $current_page = 'home'; 
     return view('pages.home'); 
    } 
} 

但不幸的是它給了我這個錯誤在HomeController.php線27

ErrorException:試圖讓非對象的屬性

和線27這條線

View::share('user_info',profile::where('username',Auth::user()->username)->first()); 

它似乎像這個問題是「驗證::用戶() - >用戶名」,因爲如果我手動指定的用戶名,它給了我收集我所需要的。

任何想法,請幫助嗎?

這是我用來修改認證資料的AuthenticatesUsers.php。

<?php 

namespace Illuminate\Foundation\Auth; 

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


trait AuthenticatesUsers 
{ 
    use RedirectsUsers, ThrottlesLogins; 

    /** 
    * Show the application's login form. 
    * 
    * @return \Illuminate\Http\Response 
    */ 
    public function showLoginForm() 
    { 
     return view('auth.login'); 
    } 

    /** 
    * Handle a login request to the application. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @return \Illuminate\Http\Response 
    */ 
    public function login(Request $request) 
    { 
     $this->validateLogin($request); 

     // If the class is using the ThrottlesLogins trait, we can automatically throttle 
     // the login attempts for this application. We'll key this by the username and 
     // the IP address of the client making these requests into this application. 
     if ($this->hasTooManyLoginAttempts($request)) { 
      $this->fireLockoutEvent($request); 

      return $this->sendLockoutResponse($request); 
     } 

     $credentials = $this->credentials($request); 

     if ($this->guard()->attempt($credentials, $request->has('remember'))) { 
      return $this->sendLoginResponse($request); 
     } 

     // If the login attempt was unsuccessful we will increment the number of attempts 
     // to login and redirect the user back to the login form. Of course, when this 
     // user surpasses their maximum number of attempts they will get locked out. 
     $this->incrementLoginAttempts($request); 

     return $this->sendFailedLoginResponse($request); 
    } 

    /** 
    * Validate the user login request. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @return void 
    */ 
    protected function validateLogin(Request $request) 
    { 
     $this->validate($request, [ 
      $this->username() => 'required', 'password' => 'required', 
     ]); 
    } 

    /** 
    * Get the needed authorization credentials from the request. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @return array 
    */ 
    protected function credentials(Request $request) 
    { 
     return $request->only($this->username(), 'password'); 
    } 

    /** 
    * Send the response after the user was authenticated. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @return \Illuminate\Http\Response 
    */ 
    protected function sendLoginResponse(Request $request) 
    { 
     $request->session()->regenerate(); 

     $this->clearLoginAttempts($request); 

     return $this->authenticated($request, $this->guard()->user()) 
       ?: redirect()->intended($this->redirectPath()); 
    } 

    /** 
    * The user has been authenticated. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @param mixed $user 
    * @return mixed 
    */ 
    protected function authenticated(Request $request, $user) 
    { 
     //check user role 
     return redirect('/app/system/dashboard'); 
    } 

    /** 
    * Get the failed login response instance. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @return \Illuminate\Http\Response 
    */ 
    protected function sendFailedLoginResponse(Request $request) 
    { 
     return redirect()->back() 
      ->withInput($request->only($this->username(), 'remember')) 
      ->withErrors([ 
       $this->username() => Lang::get('auth.failed'), 
      ]); 
    } 

    /** 
    * Get the login username to be used by the controller. 
    * 
    * @return string 
    */ 
    public function username() 
    { 
     return 'username'; 
    } 

    /** 
    * Log the user out of the application. 
    * 
    * @param Request $request 
    * @return \Illuminate\Http\Response 
    */ 
    public function logout(Request $request) 
    { 
     $this->guard()->logout(); 

     $request->session()->flush(); 

     $request->session()->regenerate(); 

     return redirect('/'); 
    } 

    /** 
    * Get the guard to be used during authentication. 
    * 
    * @return \Illuminate\Contracts\Auth\StatefulGuard 
    */ 
    protected function guard() 
    { 
     return Auth::guard(); 
    } 
} 
+0

如果Auth :: user沒有被定義(登錄)以防止那種錯誤'if(!Auth :: check()){ redirect('/ login')可能只是重定向回登錄頁面; }' – Beginner

+0

@NewbeeDev:爲什麼我必須這樣做? –

+0

你得到的Auth :: user(),但你沒有登錄Auth :: user返回空。 Auth ::用戶將只填寫,如果你登錄 – Beginner

回答

1

看來你運行該代碼時,不登錄的用戶,那麼你可以做一個檢查:

View::share('user_info', auth()->check() ? profile::where('username', auth()->user()->username)->first() : null); 

如果用戶沒有登錄該代碼將返回輪廓或null

+0

我確定我已經登錄。我試過了你的答案,但它不會產生任何錯誤,但我試圖從視圖刀片中調用它,如「{{$ user_info-> username}}」,它會引發我這個問題錯誤「試圖獲得非對象的屬性」任何想法? –

+0

@CodeDemon,你應該檢查'dd(auth() - > check())'返回。如果它返回'false',那麼你有一些認證相關的問題(你認爲用戶登錄了,但不是)。 –

+0

我嘗試'dd(auth() - > check())'給構造函數,它返回false,我試圖把它放在'public index()'中,它返回'true'任何想法?這是令人困惑的。爲什麼當我在構造函數中'dd'時,它返回false,但如果'dd'到'public index()',它會返回true? –

0

改變這種

if ($this->guard()->attempt($credentials, $request->has('remember'))) { 
    return $this->sendLoginResponse($request); 
} 

if (Auth::attempt($credentials, $request->has('remember'))) { 
    return $this->sendLoginResponse($request); 
} 
+0

會,可悲的是同樣的錯誤。 :( –

+0

@CodeDemon錯誤是什麼? – Beginner

相關問題