2017-07-18 59 views
0

我有2個登錄與不同的控制器/模型。對於contender_user 2 - for enterprise_user。 雖然它們看起來與企業用戶的文件完全相同,我不能達到enterprise_user的IDAuth :: user() - > id不會工作Laravel 5.2 /試圖獲取非對象的屬性

  1. 文件,沒有錯誤:

    namespace App\Http\Controllers\contender_user; 
    
    use Illuminate\Http\Request; 
    use Illuminate\Support\Facades\DB; 
    use Illuminate\Support\Facades\Session; 
    use Illuminate\Support\ServiceProvider; 
    
    use App\Http\Requests; 
    use App\Http\Controllers\Controller; 
    use App\Models\contender_user; 
    use Illuminate\Support\Facades\Auth; 
    
    
    class contender_userController extends Controller 
    { 
    
    
        public function applications_contender_user_show() { 
         return view('contender_user/applications'); 
        } 
    
    
        public function home_show() { 
    
         $id = Auth::user()->id; 
         $contender_user = new contender_user\contender_userModel(); 
    
         $results = $contender_user->get_user_all($id); 
    
    
         return view('contender_user/home', array 
          (
           'name' => $results[0]->name, 
           'email' => $results[0]->email, 
           'created_at' => $results[0]->created_at 
          )); 
    
        } 
    } 
    
  2. 文件有錯誤: 行:$ ID = Auth :: user() - > id; 錯誤:試圖當我複製它還是對錯誤信息的工作文件,以獲得非對象

    namespace App\Http\Controllers\enterprise_user; 
    
    
    use Illuminate\Http\Request; 
    use Illuminate\Support\Facades\DB; 
    use Illuminate\Support\Facades\Session; 
    use Illuminate\Support\ServiceProvider; 
    use Illuminate\Support\Facades\Auth; 
    use App\Http\Requests; 
    use App\Http\Controllers\Controller; 
    use App\Models\enterprise_user; 
    
    class enterprise_userController extends Controller 
    { 
    public function applications_show() { 
        return view('enterprise_user/applications'); 
    } 
    
    public function home_show() { 
    
        $id = Auth::user()->id; 
        $enterprise_user = new enterprise_user\enterprise_userModel(); 
    
        $results = $enterprise_user->get_user_all($id); 
    
    
        return view('enterprise_user/home', array 
         (
          'name' => $results[0]->name, 
          'email' => $results[0]->email, 
          'created_at' => $results[0]->created_at 
         ) 
        ); 
    
    } 
    

的財產:(

請幫助

+0

你使用任何警衛? –

+0

在使用Auth :: user()之前檢查用戶會話是否存在(Auth :: check()) –

+0

'$ id = Auth :: id();'也會有效。 – Chris

回答

0

對於Laravel 5.2可以獲得認證的用戶如下:

Auth::guard('user')->user()->id //guard can be user,admin etc depend on your settings 
+0

php風暴說使用方法「守衛」沒有找到我也有線:使用Illuminate \ Support \ Facades \ Auth; – mbrech

0

試試這個

public function home_show() { 
    if($id){ 
    $enterprise_user = new enterprise_user\enterprise_userModel(); 

    $results = $enterprise_user->get_user_all($id); 


    return view('enterprise_user/home', array 
     (
      'name' => $results[0]->name, 
      'email' => $results[0]->email, 
      'created_at' => $results[0]->created_at 
     ) 
    ); 
} 
else{ 
    ///what ever you want to do 
} 

} 
相關問題