I want to get user id in laravel 5.3 controller with Auth::user()->id but it creates error their

前端 未结 1 416
余生分开走
余生分开走 2021-01-28 04:48

I have created a points table and there users points will be debited credited. the structure is like following. id userid p_add p_less description

相关标签:
1条回答
  • 2021-01-28 05:21

    Firstly lets hope you have a users table with the primary key id and a modal associated with it in your project. Try to use 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
        }
    
    0 讨论(0)
提交回复
热议问题