试图获得非对象的属性

时间:2016-03-21 10:24:11

标签: php laravel authentication

在这里,我有一个功能,用户注册到我的网站后,它将重定向到路径(' user.show')。用户还可以在网址中搜索另一个用户。如果用户存在( ' user.show')将提供所请求用户的信息。但是,如果用户搜索无效名称,它将提供当前登录用户(正在搜索的用户)信息。当我尝试时对于无效的用户我得到错误。错误在以下行

$info=User::where($indicator,'=', Auth::user()->$indicator)->get()->first();
用户控制器中的

Show()方法:

public function show($user)
    {
        //
        $indicator=is_numeric($user)?'user_id':'username';
        $info=User::where($indicator,'=',$user)->get()->first();
        if($info){
           echo 'bal';
           $data=array('info'=>$info);
           return View::make('user.show')->with('info',$data);
        }else{
          echo "this user doesn't exist";

          $info=User::where($indicator,'=', Auth::user()->$indicator)->get()->first();

          $data=array('info'=>$info);
          return View::make('user.show')->with('info',$data);
        }
    }

2 个答案:

答案 0 :(得分:0)

尝试这样的事情:

if(Auth::user()){
    // User is authenticated
    $info = User::where($indicator,'=', Auth::user()->$indicator)->get()->first();
}else{
    // User is not authenticated
}

而不是:

$info=User::where($indicator,'=', Auth::user()->$indicator)->get()->first();

答案 1 :(得分:0)

尝试

if(Auth::check()){
    $info = User::where($indicator,'=', Auth::user()->$indicator)->first();
}else{
   // Other code goes hr
}
相关问题