Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging out via a link in Laravel

I have a "Logout" link in my top navigation bar. I'm wondering how I can make it so that while I'm logged in, it'll log me out when I click on it and return me to the homepage.

To be specific, what changes to which files do I make in Laravel? Also, what code do I need to write in the view, which currently contains just HTML, to trigger this?

like image 390
Ben Kao Avatar asked Sep 02 '25 14:09

Ben Kao


1 Answers

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>
like image 155
Lucas Bustamante Avatar answered Sep 05 '25 03:09

Lucas Bustamante