php - Laravel 5.4 $error 未显示/闪烁
问题描述
我使用 vagrant for windows 全新安装了 Laravel 5.4。
现在,当我按照 laracast 中的 tuts 并尝试表单验证时,一切都很好,但$errors变量根本不包含任何错误消息。
片段来自PostsController
public function store()
{
$this->validate(request(), [
'title' => 'required|unique:posts,title|min:3',
'body' => 'required'
]);
Post::firstOrCreate(request(['title', 'body']));
return redirect()->route('create-post');
}
我的create.blade.php的片段
@extends('layout')
@section('content')
<main role="main" class="container">
<div class="row">
<div class="col-md-8 blog-main">
<h3 class="pb-3 mb-4 font-italic border-bottom">
Create Post Form
</h3>
<form action="/api/posts" method="POST">
{{ csrf_field() }}
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" id="title" placeholder="Title" name="title" value="">
<small class="text-danger">{{ $errors->first('title') }}</small>
</div>
<div class="form-group">
<label for="body">Content</label>
<textarea class="form-control" id="body" rows="5" name="body" placeholder="Contents Here.." value=""></textarea>
<small class="text-danger">{{ $errors->first('body') }}</small>
</div>
<div class="form-group">
<button class="btn btn-primary" type="submit">Submit</button>
</div>
<div class="form-group">
<div class="alert alert-{{ $errors->any() ? 'danger' : 'default' }}">
<ul>
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
</div>
</form>
</div>
@include('partials.sidebar')
</div>
</main>
@endsection
验证失败后的结果
我有什么遗漏或有什么问题吗?
编辑
这是我的路线片段:
Route::get('/', 'PostsController@index');
Route::get('/posts/{post}', 'PostsController@show');
Route::get('/create', function() {
return view('posts.create');
})->name('create-post');
Route::post('/posts', 'PostsController@store');
解决方案
找到了答案。
因为它是 5.4,所以我没有注意到所有的 web.php 都在一个名为web的中间件下
其中包括\Illuminate\View\Middleware\ShareErrorsFromSession::class
我的api/posts在api中间件上,这就是为什么没有任何会话共享的原因。
这个链接也帮助解决了这个问题。
推荐阅读
- bash - 如何在没有 Docker 的情况下验证 Docker 登录?
- python - 如何从python中的n个多维数组中删除填充
- reactjs - 渲染问题在 reactjs 中不起作用?
- javascript - 自动生成文档并将文档文本替换为表格中的文本
- javascript - Javascript - Uncaught (in promise) TypeError: Failed to construct 'Blob': The object must have a callable @@iterator property
- python - Python 2/3 文件夹包重定向 [跨 python 版本控制]
- mysql - Debezium MySQL 连接器未显示更改事件键
- javascript - TypeError:无法读取未定义 Discord.js 事件 guildMemberAdd 的属性“发送”
- firebase - Firebase 和身份验证定价
- javascript - 是否可以为 svg 文本属性设置动画/过渡?