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

nhayder's avatar
Level 13

Event To store user IP on pivot Table on login

im using laravel login event as mentioned in the documentation, i added it to EventServiceProvider like this

    protected $listen = [
        'Illuminate\Auth\Events\Login' => [
            'App\Listeners\Accounts\StoreUserIpOnLogin', // this is the new Listener class class
        ],
    ];

this is how the listener looks like

<?php

namespace App\Listeners\Accounts;

use Auth;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Auth\Events\Login;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class StoreUserIpOnLogin
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    /**
     * Handle the event.
     *
     * @param  Login  $event
     * @return void
     */
    public function handle(Login $event)
    {
    // this should save the data to pivot table, but unfortunately  im getting an error
        $user = $event->user;

        $user = new User();

        $user->user_id = Auth::user()->id;
        $user->code = $this->request->ip();
        $user->ips()->save($user);
    }
}

the IP should be stored in a pivot table where im have already made the relationship in the user controller like this

    public function ips()
    {
        return $this->hasMany(Userip::class);
    }

this is the migration of the Userip table

        Schema::create('userips', function (Blueprint $table) {
            $table->increments('id');
            $table->string('code');
            $table->integer('user_id');
            $table->timestamps();
        });

this is the userip migration

        Schema::create('userips', function (Blueprint $table) {
            $table->increments('id');
            $table->string('code'); // i need to store the code example : 127.1.1.1
            $table->integer('user_id'); // this is the auth::user()->id
            $table->timestamps();
        });

With configuration above im not being able to save the ip +user id on the pivot table and im getting this error

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user_id' in 'field list' (SQL: insert into `users` (`user_id`, `code`, `updated_at`, `created_at`) values (, 127.0.0.1, 2018-08-10 05:22:30, 2018-08-10 05:22:30))

any ideas on how to fix this

0 likes
2 replies
tykus's avatar
tykus
Best Answer
Level 104

im have already made the relationship in the user controller

In the User model, right?

In the event listener, you are newing up a User instance rather than a Userip instance

        $user = $event->user;

        $user = new User(); // here!

        $user->user_id = Auth::user()->id;
        $user->code = $this->request->ip();
        $user->ips()->save($user);

You can fix this and simplify it:

        auth()->user()->ips()->create([
        'code' => request()->ip()
    ]);

By the way, you do not have a pivot table; it is a regular hasMany relationship here.

1 like

Please or to participate in this conversation.