jeudi 26 décembre 2019

Auth user not added

This code is work fine but when i use auth::id than show error like this in api Message unauthenticated

API

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

Working code

 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);
        
    }

Not working code, error message unauthenticated

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);
        
    }

How can i add sender_id is authenticated user id?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire