I have 2 users which is seller and user. Login function for both is fine but when I login to seller there is no logout button.
Route:
Route::post('/logout', 'Auth\PsController#logout');
psblade:
<ul class="nav navbar-nav navbar-right">
<!-- Authentication Links -->
#if (Auth::guest())
<li>Loginnn</li>
<li>Registerr</li>
#else
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
{{ Auth::user()->name }} <span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<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>
</li>
</ul>
</li>
#endif
Related
I am a newbie to Laravel. The logout option is not working for me in Laravel. It shows Login and Home Menus. After I logged in it shows the username with a down arrow, but the dropdown is not working. What could be the issue?
This is my app.blade.php
<nav class="navbar navbar-expand-md navbar-light bg-white shadow-sm">
<div class="container">
<a class="navbar-brand" href="{{ url('/') }}">
{{ config('app.name', 'Laravel') }}
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="{{ __('Toggle navigation') }}">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<!-- Left Side Of Navbar -->
<ul class="navbar-nav mr-auto">
</ul>
<!-- Right Side Of Navbar -->
<ul class="navbar-nav ml-auto">
<!-- Authentication Links -->
#guest
<li class="nav-item">
<a class="nav-link" href="{{ route('login') }}">{{ __('Login') }}</a>
</li>
#if (Route::has('register'))
<li class="nav-item">
<a class="nav-link" href="{{ route('register') }}">{{ __('Register') }}</a>
</li>
#endif
#else
<li class="nav-item dropdown">
<a id="navbarDropdown" class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre>
{{ Auth::user()->name }}
</a>
<div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="{{ route('logout') }}"
onclick="event.preventDefault();
document.getElementById('logout-form').submit();">
{{ __('Logout') }}
</a>
<a class="dropdown-item" href="{{route('admin.users.index')}}">
User Management
</a>
<form id="logout-form" action="{{ route('logout') }}" method="POST" class="d-none">
{{ csrf_field() }}
</form>
</div>
</li>
#endguest
</ul>
</div>
</div>
</nav>
I'm working on a Laravel project and I added a link to log out in my sidebar
<a class="dropdown-item" href="{{ route('logout') }}"
onclick="event.preventDefault();
document.getElementById('logout-form').submit();">
{{ __('Se déconnecter') }}
</a>
<form id="logout-form" action="{{ ('logout') }}" method="POST" style="display: none;">
#csrf
</form>
but Once I click on the link I get View [login] not found.
can you help ?
Update
update 2
I put the logout link in the view of my sidebar
<div class="navbar-default sidebar" role="navigation">
<div class="sidebar-nav navbar-collapse">
<ul class="nav" id="side-menu">
<li>
<i class="fa fa-dashboard fa-fw"></i> Dashboard
</li>
<li>
<i class="fa fa-bar-chart-o fa-fw"></i> After sales & Parts<span class="fa arrow fa-fw"></span>
<ul class="nav nav-second-level">
<li>
Obeya
</li>
<li>
Parts
</li>
</ul>
<!-- /.nav-second-level -->
</li>
<li>
<i class="fa fa-usd fa-fw"></i> Finances
</li>
<li>
<i class="fa fa-lg fa-cog fa-fw"></i> Paramétrages
</li>
<li>
<a href="{{ route('logout') }}"
onclick="event.preventDefault();
document.getElementById('logout-form').submit();">
<i class="fa fa-sign-out fa-fw"></i>
{{ __('Se déconnecter') }}
</a>
<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
#csrf
</form>
</li>
</ul>
</div>
<!-- /.sidebar-collapse -->
I think you missed route function in your form!
You should change:
<form id="logout-form" action="{{ ('logout') }}" method="POST" style="display: none;">
#csrf
</form>
to
<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
#csrf
</form>
For more details check Auth Code
UPDATE:
OP was redirecting to login view after logout but OP didn't called the view correctly in his routes.
In OP web.php the return code was like:
return view('login');
So after changing the above code to below it started working
return view('auth.login');
Because when we run php artisan make:auth, the login and other files are inside auth directory!
you have to name your route as "login"
example
Route::get('/userlogin',['as'=>'login','uses'=>'yourcontroller#yourmethod']);
or
Route::get('/userlogin', function () {
//
})->name('login');
I can not get out of my application, I'm using the auth out of box login for laravel 5, but when I get out of my account, I'm not successful.
EDIT: the problem is that dropdown menu does not open for me to logout.
Can someone help me ?
this is my app.blade.php ->
#guest
<li>Login</li>
<li>Register</li>
#else
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false" aria-haspopup="true" v-pre>
{{ Auth::user()->name }} <span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li>
<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>
</li>
</ul>
</li>
#endguest
#theFlash So CSS is setting the logout button to style="display: none" you need to set this to either display: block;, display: inline-block; or display: inline; to get it to show up. At the moment it's hidden by CSS so you can't click it...
Adding app.js file helped me to open the dropdown
//app.blade.php
<script src="{{ asset('js/app.js') }}"></script>
I want to make my index page with both guest and auth functions.
When guests visit, they could try logging in; And after login, it will show the user's info such as username.
Route routes/web.php:
Route::get('vendor', function () {
return view('vendor.home');
})->name('vendor')->middleware('web');
in blade template i use Auth::check() to authentication but failed.if i use middleware(['web','auth:vendor']) if guest will redirect to login page
#if(Auth::guest())
<li> <strong><i class="fa fa-user"></i> Login </strong> </li>
<li> <strong>Register</strong> </li>
#else
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
{{ Auth::user()->name }} <span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
Log Out
</li>
</ul>
</li>
#endif
{{ dd(Auth::check()) }}
Every time I logged in successfully, it will still show login button instead of user name button, after I REFRESH the index page.
this result
try replace Auth:: with \Auth:: or auth()
#if(\Auth::guest())
<li> <strong><i class="fa fa-user"></i> Login </strong> </li>
<li> <strong>Register</strong> </li>
#else
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
{{ \Auth::user()->name }} <span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
Log Out
</li>
</ul>
</li>
#endif
{{ dd(\Auth::user()) }}
This my answer
#if(Auth::guard('vendor')->check())
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
{{ Auth::guard('vendor')->user()->name }} <span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
Dashboard
</li>
<li>
<a href="{{ route('vendor.logout') }}" >Log Out</a>
</li>
</ul>
</li>
#else
<li> <strong><i class="fa fa-lock"></i> Login </strong> </li>
<li> <strong>Register</strong> </li>
#endif
I am new to laravel5.4. By using this url i am trying to make user management. After installing everything i got below error. Please help this out to fix this error
(3/3) ErrorException
Call to undefined method Illuminate\Database\Query\Builder::hasRole() (View: /var/www/html/laravel5/resources/views/layouts/app.blade.php) (View: /var/www/html/laravel5/resources/views/layouts/app.blade.php)
My layout app.blade.php is as below
<ul class="nav navbar-nav navbar-right">
<!-- Authentication Links -->
#if (Auth::guest())
<li>Login</li>
<li>Register</li>
#else
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
{{ Auth::user()->name }} <span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
#role('Admin') {{-- Laravel-permission blade helper --}}
<i class="fa fa-btn fa-unlock"></i>Admin
#endrole
<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>
</li>
</ul>
</li>
#endif
</ul>