Why laravel defalut logout dropdown is not working? - php

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>

Related

Laravel annoying css loading

Im developing a blog in laravel hosted in apache. Im using laravel mix for compiling my assets (css, js).
When I go to some route, the page takes less than 1 second to charge the css:
The elapsed time is very short, however it is very annoying. Why does this happen?
I know that the answer can come from many factors and the only thing that occurs to me is something wrongly configured in laravel mix but my js and css assets only have a couple of codes.
For example this also happens on my home page (index) which have a very simple code (except for the menu bar). the code of my Home page:
home.blade.php
<html>
#extends('layout/layout')
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Blog</title>
</head>
<body>
<div class="contenedor">
#include('menu/menu-nav')
<h1>Bienvenidos al Blog!</h1>
</div>
</body>
</html>
layout.blade.php
<script src="{{ asset('js/app.js') }}"></script>
<link rel="stylesheet" href="{{ asset('css/app.css') }}">
menu-nav.blade.php
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
#if(Auth::check())
<a class="navbar-brand" href={{ route('editProfile')}}>
{{ Auth::user()->username }}
</a>
#endif
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarText" aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarText">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href={{ route('home') }} >Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href={{ route('viewposts') }}>Blog</a>
</li>
#if(Auth::user())
#if(Auth::user()->hasRole('creator'))
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Posts
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<a class="dropdown-item" href={{ route('createpost') }}>Crear Post</a>
<a class="dropdown-item" href="#">Editar Post</a>
</div>
#endif
#endif
#if(Auth::user())
#if(Auth::user()->hasRole('admin'))
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Administración
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<a class="dropdown-item" href="{{ route('viewusers') }}">Usuarios</a>
<a class="dropdown-item" href="#">Estadísticas</a>
</div>
#endif
#endif
</ul>
#if(Auth::user())
<span class="navbar-text">
<a class="nav-link" href={{ route('logout') }}>Log out</a>
</span>
#else
<span class="navbar-text">
<a class="nav-link" href={{ route('register') }}>Registrarse</a>
</span>
<span class="navbar-text">
<a class="nav-link" href={{ route('login') }}>Log in</a>
</span>
#endif
</div>
I would recommend you place the CSS and JS tags into the <head> section, as explained here: https://stackoverflow.com/a/18392465/14481105 the browser looks for those files inside the head section.
I'm not sure if you have just omitted it for Stack Overflow purposes but you also want the <html> tags and <body> tags.

How to pass data to my header in my layouts in laravel?

<nav class="navbar navbar-expand-lg navbar-light fixed-top" id="mainNav">
<div class="container">
<a class="navbar-brand" href="{{ route('home.index') }}">En</a>
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse"
data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false"
aria-label="Toggle navigation">
Menu
<i class="fas fa-bars"></i>
</button>
<div class="collapse navbar-collapse" id="navbarResponsive">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="{{ route('home.index') }}">Home</a>
</li>
<li class="dropdown nav-item">
<a class="nav-link" data-toggle="dropdown">Categories<i class="icon-arrow"></i></a>
<ul class="dropdown-menu">
#foreach($categories as $category)
<li>
<a href="/?category={{ $category->name }}">
{{ $category->name }}
</a>
</li>
#endforeach
</ul>
</li>
<li class="nav-item">
<a class="nav-link" href="{{ route('about') }}">About Me</a>
</li>
<li class="nav-item">
<a class="nav-link" href="post.html">Contact</a>
</li>
</ul>
</div>
</div>
</nav>
I have the above code in my header which I include in my app.blade.php as #include('user.layouts.header') then I use app.blade.php as my layout.
How do I pass data to the header so that I can stop getting the below error if that's the reason I'm getting the error!
Undefined variable: categories (View: /home/alphy/blogEngidaFinal/resources/views/user/layouts/header.blade.php)
See the documentation.
You can pass params while including the header view,
#include('view.name', ['some' => 'data'])
In your case,
#include('user.layouts.header', ['categories' => ['food', 'drink', 'dessert'])

Laravel : after adding make:auth getting an error in my login/register page

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 :)

Laravel hasRole() error while creating user management

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>

Multi-auth not working 5.4

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

Categories