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