2016-03-21 64 views
0

這裏我有一個功能,其中用戶註冊到我的網站後,它將重定向到路徑('user.show')。用戶也可以搜索另一個用戶在url.If用戶存在('user.show')將提供請求的用戶的信息。但是如果用戶搜索一個無效的名字,它會提供當前登錄的用戶(正在搜索的用戶)信息。當我嘗試給一個無效的用戶時,的和error.The誤差是在下面的行試圖獲得非對象的財產

$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); 
     } 
    } 
+0

什麼'$ indicator'的回報,當你試圖調用'驗證: :用戶() - > $ indicator'? –

+0

它看起來像Auth :: user() - > $指示器不是一個有效的對象...它給我錯誤,如果我回聲Auth :: user() - > $ indicator –

+0

檢查什麼'dd($指標);'回報。 –

回答

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(); 
+0

我想要做的是,如果搜索用戶不存在,它會返回登錄用戶 –

0

嘗試

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

dd(Auth :: user())是NULL ..我該怎麼辦? –

+0

你把dd(Auth :: user())放在哪裏?它是否在if條件內? – oseintow

+0

在我的代碼有問題在其他條件 –

相關問題