Create migration for login_attempts table
MODIFY
app/Http/Controllers/LoginController.php
app/Http/Controllers/LoginController.php
namespace App\Http\Controllers;
+use App\LoginAttempt;
+use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
@@ -17,10 +19,24 @@ class LoginController extends Controller {
])) {
$user = Auth::user();
+ LoginAttempt::create([
+ 'user_id' => $user->id,
+ 'ip' => $request->ip(),
+ 'failed' => false
+ ]);
+
session(['space' => $user->spaces[0]]);
return redirect()->route('dashboard');
} else {
+ $user = User::where('email', $request->input('email'))->first();
+
+ LoginAttempt::create([
+ 'user_id' => $user ? $user->id : null,
+ 'ip' => $request->ip(),
+ 'failed' => true
+ ]);
+
$request->flash();
return redirect()

MODIFY
app/Http/Controllers/RegisterController.php
app/Http/Controllers/RegisterController.php
@@ -56,6 +56,12 @@ class RegisterController extends Controller {
Auth::loginUsingId($user->id);
+ LoginAttempt::create([
+ 'user_id' => $user->id,
+ 'ip' => $request->ip(),
+ 'failed' => false
+ ]);
+
return redirect()
->route('dashboard');
}

ADD
app/LoginAttempt.php
+<?php
+
+namespace App;
+
+use Illuminate\Database\Eloquent\Model;
+
+class LoginAttempt extends Model {
+ protected $fillable = ['user_id', 'ip', 'failed'];
+}

ADD
database/migrations/2019_01_02_230218_create_login_attempts_table.php
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class CreateLoginAttemptsTable extends Migration {
+ public function up() {
+ Schema::create('login_attempts', function (Blueprint $table) {
+ $table->increments('id');
+ $table->unsignedInteger('user_id')->nullable();
+ $table->string('ip');
+ $table->boolean('failed')->default(false);
+ $table->timestamps();
+
+ $table->foreign('user_id')->references('id')->on('users');
+ });
+ }
+
+ public function down() {
+ Schema::table('login_attempts', function ($table) {
+ $table->dropForeign('login_attempts_user_id_foreign');
+ });
+
+ Schema::dropIfExists('login_attempts');
+ }
+}

Create migration for login_attempts table