Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

anonymox's avatar

How to get client ip address in register controller in laravel 5.3 with passport enabled

Hi i want to get the client ip address in my laravel project when registering, I've found some solutions that says get the ip using request global function like :

Protected function create(array $data, Request $request){
Return user::create([
'name' => $data['name'],
'ip' => $request->ip();
]);
}

But because I'm using passport in my project and it added guzzel to the project when i add :

Use Illuminate\Http\Request; 

To my register controller it gives me an error message of conflict with guzzel Request.

How can i solve this?

0 likes
6 replies
anonymox's avatar
anonymox
OP
Best Answer
Level 1

I've managed to solve this problem using : \Request::ip()

   protected function create(array $data)
    {
        return User::create([
            'role' => $data['role'],
            'sex' => $data['gender'],
            'fullname' => $data['fullname'],
            'mobile' => $data['mobile'],
            'mobile_prefix' => $data['mobile_prefix'],
            'email' => $data['email'],
            'co_name' => $data['co_name'],
            'tel' => $data['tel'],
            'tel_prefix' => $data['tel_prefix'],
            'password' => bcrypt($data['password']),
            'balance' => $data['balance'],
            'last_ip' => \Request::ip(),
        ]);

    }
2 likes
Edelco's avatar

Thanks @anonymox .. I needed that too and works. I use it when an user activates his/her account.

oroalej's avatar

You can also use request()->ip()

1 like
YasirArfat's avatar

I don't know why This doesn't work for me. I tried both \Request::ip() and request()->ip() but both cases returns null(added the namespace too). Is that because I'm on localhost? it atleast should return ::1 or 127.0.0.1 i guess.

Mateoo's avatar

You need to also add this new column to $fillable in app/http/User.php model

protected $fillable = [
    'name', 'email', 'password', 'user_ip',
];

By default only 'name', 'email', 'password' is there.

Please or to participate in this conversation.