Laravel get ip address

2019-12-10 16:19:26 Suresh Laravel

How can we get the IP address of the user in Laravel

Hello friend in this post I going to show you how to get users IP address in Laravel 5.8 and save it is database.

Different Methods of Laravel to get IP address

1. $clientIP = \Request::ip();
2. $clientIP = \Request::getClientIp(true);
3. $clientIP = request()->ip();
4. public function index(Request $request){
  dd($request->ip());
}

Laravel get IP address and Save it in Database

public function trackUserActivity(Request $request){
  
  $user_activity=new UserActivity();  
  // getting logged in user info from laravel Auth
   
  $user_activity->user_id=Auth::id();
  
  // Getting ip address of remote user
  
  $user_activity->ip_address=$request->ip();
   
  $user_activity->save();
   
}

  

This post is submitted by one of our members. You may submit a new post here.

Related Tricks