Hello Artisan,
In this laravel 8 logout redirect tutorial, I will show you how we can create our own custom logout system in Laravel application. Though laravel provides a default authentication system with login registration logout reset password etc.
But in this example, we will see how we can create our own logout system in Laravel. I will share it with you step by step so that you can understand. So let's start our example of custom logout system in laravel. I will also show you how to redirect after logout.
Step 1: Create Route
In this step, we need a post route to create logout system. So create it like below:
routes/web.php
Route::post('logout', [UserController::class, 'logout'])->name('logout');
Step 2: Create Controller
In this step, we need a controller to write our logout logic. So create it and update it with this code:
app/Http/Controllers/UserController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
use Validator;
use Auth;
class UserController extends Controller
{
public function logout(Request $request) {
Auth::logout();
return redirect()->route('login');
}
}
Step 3: Create Logout form
In this step, we need a logout form to hit a logout button. So create a blade file with logout form like that.
Recommended: Laravel 8 Custom Login and Registration Example
Hope it can help you.
#laravel #laravel-8x