[英]I want to get user id in laravel 5.3 controller with Auth::user()->id but it creates error their
I have created a points
table and there users points will be debited credited. 我已经创建了一个
points
表,那里的用户积分将被记入贷方。 the structure is like following. 结构如下。
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-
and I am using following controller code. 我正在使用以下控制器代码。
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));
}
so in view page named as account page result is correctly displayed as I want. 因此,在名为“帐户页面”的视图页面中,结果可以正确显示。 But this query is getting user_id as a static value.
但是此查询获取的是user_id作为静态值。 I want it dynamically.
我要动态地。 So if I put
Auth::user()->id
in the place of 9 nothing is displayed even I have used use Auth;
因此,如果我将
Auth::user()->id
放在9的位置,即使我已经使用use Auth;
也不会显示任何内容use Auth;
at top. 在顶部。 Help me to get net points from database with dynamic user_id in query.
帮我从查询中使用动态user_id从数据库获取净点。
Firstly lets hope you have a users table with the primary key id and a modal associated with it in your project. 首先,希望您在项目中有一个具有主键ID和与之关联的模式的用户表。 Try to use
use Illuminate\\Support\\Facades\\Auth;
尝试使用
use Illuminate\\Support\\Facades\\Auth;
then 然后
// 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.