[英]I want to get user id in laravel 5.3 controller with Auth::user()->id but it creates error their
我已經創建了一個points
表,那里的用戶積分將被記入貸方。 結構如下。 id userid p_add p_less description created_at updated_at 1 9 3000 0 purchased -time- -time- 2 9 0 300 expend -time- -time-
我正在使用以下控制器代碼。
public function account_page(){
$points = Points::select(DB::raw("sum(p_add)-sum(p_less) as Total"))->where('user_id', 9)->first();
return view('mop.account-page', array('points'=>$points));
}
因此,在名為“帳戶頁面”的視圖頁面中,結果可以正確顯示。 但是此查詢獲取的是user_id作為靜態值。 我要動態地。 因此,如果我將Auth::user()->id
放在9的位置,即使我已經使用use Auth;
也不會顯示任何內容use Auth;
在頂部。 幫我從查詢中使用動態user_id從數據庫獲取凈點。
首先,希望您在項目中有一個具有主鍵ID和與之關聯的模式的用戶表。 嘗試使用use Illuminate\\Support\\Facades\\Auth;
然后
// This will check if user is logged in..
if (Auth::check()) {
$user_id = Auth::user()->id;
$points = Points::select(DB::raw("sum(p_add)-sum(p_less) as Total"))->where('user_id', $user_id)->first();
return view('mop.account-page', array('points'=>$points));
} else {
return redirect('somewhere'); // or return anything you want
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.