未添加身份验证用户

时间:2019-12-27 06:11:33

标签: laravel laravel-5 eloquent

此代码可以正常工作,但是当我使用auth :: id而不是在未经身份验证的api消息中显示此类错误

API

Route::post('/friend', 'FriendController@index')->middleware('auth');

工作代码

 public function index(Request $request) {
       
        $sender = Friend::where('sender_id', $request->sender_id)->where('receiver_id',$request->receiver_id)->first();
        if(empty($sender)){
            Friend::create(['sender_id'=>$request->sender_id,'receiver_id'=>$request->receiver_id, 'approved'=>'pending']);
            
            $response = ['message'=>'Friend Request has been sent','status'=>200];
            
        }else{
            $response = ['message'=>'Request has been sent already','status'=>200];
         
        }
        return response()->json($response);
        
    }

无法正常工作的代码,未验证错误消息

public function index(Request $request) {
       //$user = Auth::user()->id;
        $sender = Friend::where('sender_id', $request->Auth::user()->id)->where('receiver_id',$request->receiver_id)->first();
        if(empty($sender)){
            Friend::create(['sender_id'=>$request->Auth::user()->id,'receiver_id'=>$request->receiver_id, 'approved'=>'pending']);
            
            $response = ['message'=>'Friend Request has been sent','status'=>200];
            
        }else{
            $response = ['message'=>'Request has been sent already','status'=>200];
         
        }
        return response()->json($response);
        
    }

如何添加sender_id是经过身份验证的用户ID?

4 个答案:

答案 0 :(得分:1)

您不应该使用$request->Auth::user()->id来获取经过身份验证的用户的ID。而是直接使用它来获取ID。

Auth::user()->id

代码:

public function index(Request $request) {
       //$user = Auth::user()->id;
        $sender = Friend::where('sender_id', Auth::user()->id)->where('receiver_id',$request->receiver_id)->first();
        if(empty($sender)){
            Friend::create(['sender_id'=>Auth::user()->id,'receiver_id'=>$request->receiver_id, 'approved'=>'pending']);

            $response = ['message'=>'Friend Request has been sent','status'=>200];

        }else{
            $response = ['message'=>'Request has been sent already','status'=>200];

        }
        return response()->json($response);

    }

答案 1 :(得分:1)

auth()->id()

$request->user()->id

这将为您提供登录用户的ID

还要检查您的中间件应该是

->middleware('auth:api')

答案 2 :(得分:0)

尝试更改

$request->Auth::user()->id 

Auth::user()->id

答案 3 :(得分:0)

尝试

Auth::guard('api')->user()->id;而不是Auth::user()->id;

public function index(Request $request) {
        $user = Auth::guard('api')->user()->id;
        $sender = Friend::where('sender_id', $user)->where('receiver_id',$request->receiver_id)->first();
        if(empty($sender)){
            Friend::create(['sender_id'=>$user,'receiver_id'=>$request->receiver_id, 'approved'=>'pending']);

            $response = ['message'=>'Friend Request has been sent','status'=>200];

        }else{
            $response = ['message'=>'Request has been sent already','status'=>200];

        }
        return response()->json($response);

    }