Well, even if what @Tauras offers just works, I donβt think this is the right way to handle this.
You said you ran php artisan make:auth , which also had to insert Auth::routes(); into your routes/web.php routing files. Which comes with the default logout path already set and is called logout .
You can see it here on GitHub , but I will also post the code here for simplicity:
/** * Register the typical authentication routes for an application. * * @return void */ public function auth() { // Authentication Routes... $this->get('login', 'Auth\ LoginController@showLoginForm ')->name('login'); $this->post('login', 'Auth\ LoginController@login '); $this->post('logout', 'Auth\ LoginController@logout ')->name('logout'); // Registration Routes... $this->get('register', 'Auth\ RegisterController@showRegistrationForm ')->name('register'); $this->post('register', 'Auth\ RegisterController@register '); // Password Reset Routes... $this->get('password/reset', 'Auth\ ForgotPasswordController@showLinkRequestForm ')->name('password.request'); $this->post('password/email', 'Auth\ ForgotPasswordController@sendResetLinkEmail ')->name('password.email'); $this->get('password/reset/{token}', 'Auth\ ResetPasswordController@showResetForm ')->name('password.reset'); $this->post('password/reset', 'Auth\ ResetPasswordController@reset '); }
Note again that logout requires POST as an HTTP request method. There are many good reasons for this, but one very important point is worth mentioning: in this way you can prevent cross-site request forgery .
Thus, according to what I just pointed out, the correct way to implement this could be as follows:
<a href="{{ route('logout') }}" onclick="event.preventDefault(); document.getElementById('frm-logout').submit();"> Logout </a> <form id="frm-logout" action="{{ route('logout') }}" method="POST" style="display: none;"> {{ csrf_field() }} </form>
Finally, note that I inserted the "ready" function out of the box " {{ csrf_field() }} !
source share