Logging out via a link in Laravel

When you run php artisan make:auth, the default app.php in Laravel 5.5 does it like this:

<a href="{{ route('logout') }}" onclick="event.preventDefault(); document.getElementById('logout-form').submit();">
    Logout
</a>

<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
    {{ csrf_field() }}
</form>

Edited 28/12/2019: It's work, but This answer contains a serious security issue. Please consider before using it. The Answer by Lucas Bustamante maybe a better choice. Refer to the comment section of this answer.

1) if you are using the auth scaffold that laravel contains. You can do this, in your navigation bar add this:

<a href="{{ url('/logout') }}"> logout </a>

then add this to your web.php file

Route::get('/logout', '\App\Http\Controllers\Auth\LoginController@logout');

Done. This will logout you out and redirect to homepage. To get the auth scaffold, from command line, cd into your project root directory and run

php artisan make:auth 

2) add this to your navigation bar:

<a href="{{ url('/logout') }}"> logout </a>

then add this in your web.php file

Route::get('/logout', 'YourController@logout');

then in the YourController.php file, add this

public function logout () {
    //logout user
    auth()->logout();
    // redirect to homepage
    return redirect('/');
}

Done.

Read:

https://mattstauffer.co/blog/the-auth-scaffold-in-laravel-5-2
https://www.cloudways.com/blog/laravel-login-authentication/