在routes.php中
Route::get('/form1', 'FriendsController@getAddFriend');
Route::post('/form1', 'FriendsController@postAddFriend');
在app / Http / Controllers / FriendsController.php
中namespace App\Http\Controllers;
use App\Http\Requests\FriendFormRequest;
use Illuminate\Routing\Controller;
use Response;
use View;
class FriendsController extends Controller
{
public function getAddFriend()
{
return view('friends.add');
}
public function postAddFriend(FriendFormRequest $request)
{
return Response::make('Friend added!');
}
}
在app / Http / Requests / FriendFormRequest.php
中namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Response;
class FriendFormRequest extends Request
{
public function rules()
{
return [
'first_name' => 'required',
'email_address' => 'required|email'
];
}
public function authorize()
{
return true;
}
public function forbiddenResponse()
{
return Response::make('Permission denied foo!', 403);
}
public function response()
{
}
}
在resources / views / friends / add.blade.php
中 @foreach ($errors->all() as $error)
<p class="error">{{ $error }}</p>
@endforeach
<form method="post">
<label>First name</label><input name="first_name"><br>
<label>Email address</label><input name="email_address"><br>
<input type="submit">
</form>
当我按http://localhost/laravel/public/form1
运行时我收到错误&#34;哎呀,看起来出了问题。&#34;
当我删除以下行
时 @foreach ($errors->all() as $error)
<p class="error">{{ $error }}</p>
@endforeach
显示表单
错误是什么?
答案 0 :(得分:2)
我能想到的是你的$ errors变量不存在,这就是导致脚本抛出异常的原因。
1。如果您使用的是Laravel 5.2,可以在此找到答案: Undefined variable: errors in Laravel
基本上在 app / Http / Kernel.php 中,您需要检查 $ middlewareGroups ['web'] 是否包含
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
2. 如果您使用的是其他Laravel版本,可能还可以添加以下附加支票:
@if(isset($errors))
@foreach ($errors->all() as $error)
<p class="error">{{ $error }}</p>
@endforeach
@endif
要进一步调查此问题,您需要向我们提供异常的堆栈跟踪。如果您只看到“Whoops ...”消息,请转到 .env 文件并更改 APP_DEBUG = true < /强>