Auth :: user() - > id不会工作Laravel 5.2 /试图获取非对象的属性

时间:2017-07-18 14:06:21

标签: php laravel laravel-5.2

我有两个不同控制器/型号的登录。 1 - 对于contender_user 2 - 对于enterprise_user。 虽然它们在文件中与企业用户看起来完全相同但我无法达到enterprise_user的id

  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
            )
        );
    
    }
    
  3. 当我复制工作文件时,它仍然出现错误信息:(

    请帮助

2 个答案:

答案 0 :(得分:0)

对于Laravel 5.2,您可以通过以下方式获得经过身份验证的用户:

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

答案 1 :(得分: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
}

}