[英]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.