Laravel:添加make:auth后在我的登录/注册页面中出错
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Laravel:添加make:auth后在我的登录/注册页面中出错相关的知识,希望对你有一定的参考价值。
我做了一个laravel新闻网站。我尝试通过make:auth添加auth,但是当我点击按钮登录或注册时,它会给我一个这样的错误:
我不知道它来自哪里。它指的是我的控制器中的一个功能,但我不知道为什么它根本不需要该功能。我通常在添加授权时没有错误。有谁有同样的问题或有谁知道如何解决它?
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>
您可能正在尝试访问不存在的类别,此页面应返回404错误。
在$category = Categorie::where('naam',$category)->first();
之后
添加行
abort_if(is_null($category), 404);
它将返回404错误,因为它必须。
在我看来,问题实际上是在上面这一行。
$category = Categorie::where('naam',$category)->first();
此行未返回任何数据。尝试在此行之后放置dd($category)
以检查您的查询是否确实存在类别。
尝试像这样调试,
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());
}
}
我相信你能解决这个问题。
正如Narbil Farhan所说,问题在于:
$category = Categorie::where('naam',$category)->first();
您似乎没有从Categorie表中获得任何结果。第一个问题可能是表不存在,请注意Laravel搜索表'Categories'(搜索模型名称+ s)。如果没有返回数据,你可能应该捕获错误,如Nevermind23建议的那样。
if(isset($category->id) && $category->id != ""){
$artikels = Artikel::where('category_id', $category->id)->get()
}
个人注意事项,尽量避免使用您的母语编程。非荷兰语的说法难以理解您的代码。
有多种解决方案:
- 解:
$category = Categorie::where('naam', $category)->first();
if(#category){
//you can get the $artikels belonging to the category
}
在这种情况下,如果没有具有相应名称的类别,$ category将为null,并且不会执行IF语句中的代码。
- 解:
$category = Categorie::where('naam', $category)->firstOrFail();
//you can get the $artikels belonging to the category
在这种情况下,如果未找到类别,将抛出IlluminateDatabaseEloquentModelNotFoundException
。如果未捕获异常,则会自动将404 HTTP响应发送回用户。
- 解决方案:路线模型绑定 - 请参阅the documentation
public function oversight(Categorie $category) {
return view('pages.oversight', ['category'=>$category]);
}
在这种情况下,如果没有具有相应名称/ ID的类别,则会自动将404 HTTP响应发送回用户。
另外,我建议你在relationship和Categorie
模型之间创建一个(最有可能是OneToMany)Artikel
。它会让你的生活更简单:)
以上是关于Laravel:添加make:auth后在我的登录/注册页面中出错的主要内容,如果未能解决你的问题,请参考以下文章
Laravel 6 中未定义 Artisan 命令“make:auth”
命令“make:auth”未定义laravel 6 [重复]