In my web-app i made a profile section . But the problem is when it goes to dashboard in right-navbar it has my profile and logout. when i click on my profile-> in my profile page the right-navbar became register and sign in instead of name and logout. I think some authentication issue is there. please suggest how to fix it. check screenshots also for better understanding.
app.blade.php
<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 }} <span class="caret"></span>
</a>
<div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdown">
#auth
<a class="dropdown-item" href="{{ route('profiles.show', Auth::user()->profile->id) }}">
My Profile
</a>
#endauth
<a class="dropdown-item" 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
</form>
</div>
</li>
#endguest
</ul>
</div>
</div>
profiles/show.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">
{{ $profile->name }}
</div>
<div class="card-body">
<form id="update-profile-form" action="{{ route('profiles.update' , $profile->id) }}" method="POST" enctype="multipart/form-data">
#csrf
#method('PATCH')
<div class="form-group row justify-content-center">
<div class='profile-avatar'>
<div onclick="document.getElementById('image').click()" class="profile-avatar-overlay">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 32 32">
<path d="M18 22.082v-1.649c2.203-1.241 4-4.337 4-7.432 0-4.971 0-9-6-9s-6 4.029-6 9c0 3.096 1.797 6.191 4 7.432v1.649c-6.784 0.555-12 3.888-12 7.918h28c0-4.030-5.216-7.364-12-7.918z"></path>
</svg>
</div>
<img src="{{ URL::to('image/' .$profile->image()) }}" alt="" >
</div>
</div>
<input onchange="document.getElementById('update-profile-form').submit()" style="display: none;" id="image" type="file" name="image">
<div class="form-group">
<label for="name" class="form-control-label">
Name
</label>
<input id="name" name="name" value="{{ $profile->name }}" type="text" class="form-control">
</div>
<div class="form-group">
<label for="description" class="form-control-label">
Description
</label>
<textarea name="description" id="description" rows="3" class="form-control">{{ $profile->description }}</textarea>
</div>
<button type="submit" class="btn btn-info">
update profile
</button>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
ProfileController.php
class ProfileController extends Controller
{
public function show(Profile $profile)
{
return view('profiles.show', compact('profile'));
}
public function update(Request $request, Profile $profile)
{
if ($request->hasFile('image'))
{
$profile->clearMediaCollection('images');
$profile->addMediaFromRequest('image')
->toMediaCollection('images');
}
return redirect()->back();
}
}
i've made a laravel newswebsite. i've tried to add auth by doing make:auth, but when i click the button login or register it gives me an error like this:
i dont know where it is coming from. it refers to a function in my controller but i dont know why cause it doesnt need that function at all. i usually dont have errors while adding authorization. is there anyone who's had the same problem or anyone who knows how to fix it?
app.blade.php
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- CSRF Token -->
<meta name="csrf-token" content="{{ csrf_token() }}">
<link rel="stylesheet" href="{{asset('css/app.css')}}">
<link rel="stylesheet" href="{{asset('css/custom.css')}}">
<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
<title>{{config('app.name', 'News')}}</title>
</head>
<body>
#include('inc.navbar')
#include('inc.messages')
#yield('content')
#include('inc.footer')
</body>
navbar.blade.php
<header>
<div class="container-fluid">
<div class="row headercontent">
<div class="col-md-3 offset-md-1 borderbottom">
</div>
<div class="col-md-4 borderbottom header-logo bg-white">
<a href="{{url('/')}}"><img class="high-pic"
src="https://upload.wikimedia.org/wikipedia/commons/7/77/The_New_York_Times_logo.png"
style="width: 100%;"></a>
</div>
<div class="col-md-3 search">
<span class="fas fa-search"></span>
<input type="text" class="borderbottom backgroundtextinput"
style="position:absolute; bottom:0; width:80%;outline:none;">
</div>
</div>
<div class="row logo-NY">
<div class="col-md-6 offset-md-3 borderbottom header-logo bg-white">
<div style="z-index:3000;"></div>
<a href="{{url('/')}}"><img class="high-pic"
src="https://upload.wikimedia.org/wikipedia/commons/7/77/The_New_York_Times_logo.png"
style="width: 100%;"></a>
</div>
<div class="col-md-3">
<div id="clickme" class="d-lg-none">
<i class="fas fa-arrow-down"></i>
</div>
</div>
<div class="col-md-3">
</div>
</div>
</div>
<div class="desktop-nav">
<nav class="navbar navbar-expand-lg navbar-light font">
<div class="container">
<div class="" id="navbarSupportedContent" style="margin:auto;">
<div class="navbar-flex fontcolorblack">
#foreach($categories_navbar as $item)
<div class="nav-item">
<a class="nav-link bold" href="{{url($item->naam)}}">{{ucfirst($item->naam)}}</a>
</div>
#endforeach
#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 }} <span class="caret"></span>
</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>
<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
#csrf
</form>
</div>
</li>
#endguest
</div>
</div>
</div>
</nav>
</div>
<div id="mobile-nav" style="width: 100%; display: none; background-color: #2e3192; color: #fff;">
<div id="closeme" class="d-lg-none">
<i class="fas fa-times fa-2x"></i>
</div>
<nav class="navbar navbar-light bg-light fixed-top d-lg-none" style="z-index:999;">
<ul class="navbar-nav mr-auto">
<a href="{{url('/')}}"><img class="high-pic"
src="https://upload.wikimedia.org/wikipedia/commons/7/77/The_New_York_Times_logo.png"
style="width: 100%;"></a>
#foreach($categories_navbar as $item)
<div class="nav-item">
<a class="nav-link bold" href="{{url($item->naam)}}">{{ucfirst($item->naam)}}</a>
</div>
#endforeach
<form class="form-inline my-2 my-lg-0" id="search_mobile" action="search" method="get">
<input class="form-control mr-sm-2" name="search" type="search" placeholder="Search..."
aria-label="Search">
</form>
#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 }} <span class="caret"></span>
</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>
<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
#csrf
</form>
</div>
</li>
#endguest
</ul>
</nav>
</div>
</header>
It seems to me that the problem actually is in the above line.
$category = Categorie::where('naam',$category)->first();
This line is not returning any data. Try putting dd($category) after this line to check if there is indeed a category with your query.
Probably you are trying to access non-existing category, this page should return 404 error.
after $category = Categorie::where('naam',$category)->first();
add line
abort_if(is_null($category), 404);
and it will return 404 error as it must be.
Try to debug like this,
public function oversight($category){
try{
$getCategory = Categorie::where('naam',$category)->first();
if($getCategory !== null){
dd("I GOT SELECTED CATEGORY ROW");
}else{
dd("I THINK THERE IS NO DATA AVAIALBLE BASED ON WHERE Condition");
}
dd("ALL FINE I AM GOING BACK TO VIEW WITH DATA");
}catch(\Exception $e){
dd("I GOT THIS ERROR : ".$e->getMessage());
}
}
I'm sure you'll able to fix problem by this.
As Narbil Farhan stated, the problem is in the line:
$category = Categorie::where('naam',$category)->first();
You seem to get no result from the Categorie table. First problem can be that the table does not exist, note that Laravel searches for the table 'Categories' (Searches for model name + s). You should probably catch the error if there is no data returned, like Nevermind23 suggested.
if(isset($category->id) && $category->id != ""){
$artikels = Artikel::where('category_id', $category->id)->get()
}
Personal note, try to prevent programming in your native language. This is harder for non Dutch speakings to understand your code.
There are multiple solutions:
Solution:
$category = Categorie::where('naam', $category)->first();
if(#category){
//you can get the $artikels belonging to the category
}
In this case, if there are no category with the respective name, $category will be null, and the code inside IF statement will not be executed.
Solution:
$category = Categorie::where('naam', $category)->firstOrFail();
//you can get the $artikels belonging to the category
In this case, if the Category is not found, an Illuminate\Database\Eloquent\ModelNotFoundException will be thrown. If the exception is not caught, a 404 HTTP response is automatically sent back to the user.
Solution: Route model binding - see the documentation
public function oversight(Categorie $category) {
return view('pages.oversight', ['category'=>$category]);
}
In this case, if there is no category with the respective name/id, a 404 HTTP response is automatically sent back to the user.
Also, I'd suggest you, to create a( most probably OneToMany) relationship between the Categorie and Artikel model. It will make your life much simpler :)
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 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