12

I am new in Laravel and using laravel version 5.2.

I created a controller and request named as ArticlesController and CreateArticleRequest respectively and i defined some validation rules.

CreateArticleRequest

<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;

class CreateArticleRequest extends Request
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'title' => 'required|min:3',
            'body' => 'required|max:400',
            'published_at' => 'required|date',
        ];
    }
}

ArticlesController

<?php

namespace App\Http\Controllers;

use App\Article;
//use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Carbon\Carbon;
use App\Http\Requests\CreateArticleRequest;

class ArticlesController extends Controller
{

    public function add(){
        return view('articles.add');
    }

    public function create_article_row(CreateArticleRequest $request){
        Article::create($request->all());
        return redirect('articles/');
    }
}

When i use $errors variable in my template named as add.blade.php it show error undefined variable: $errors I tried to solve the problem but i did't .Please tell me where i am wrong . add.blad.php

{{ var_dump($errors) }}

Click here to see Error Image

4

6 回答 6

30

这是 5.2 升级的一个重大问题。正在发生的事情是负责使该errors变量可用于所有视图的中间件没有被使用,因为它已从全局中间件移动到web中间件组。

有两种方法可以解决此问题:

  1. 在您的kernel.php文件(app/Http/Kernel.php)中,您可以将其移middleware \Illuminate\View\Middleware\ShareErrorsFromSession::classprotected $middleware属性。

  2. 用路由组包装所有web路由并将 Web 中间件应用于它们:

    Route::group(['middleware' => 'web'], function() {
        // Place all your web routes here...(Cut all `Route` which are define in `Route file`, paste here) 
    });
    

从这篇文章中复制Laravel 5.2 $errors not appearing in Blade

于 2015-12-26T22:18:00.027 回答
4

发布此内容可能对其他人有用,

正如 Praveen 在第一个解决方案中提到的,在您的Kernel.phpfile( app/Http/Kernel.php)中\Illuminate\View\Middleware\ShareErrorsFromSession::class 从属性$middlewareGroups移至protected $middleware属性,但同样会开始抛出错误“会话存储未按请求设置”,

也解决这个 \Illuminate\Session\Middleware\StartSession::class,问题$middleware property

于 2016-02-09T08:03:28.807 回答
2

发生这种情况是因为下面的文件在作曲家更新过程中没有更新,所以没有mapWebRoutes实现该方法。

app/Providers/RouteServiceProvider.php

从全新安装中复制此文件,它将起作用。更好的是,遵循文档上的升级路径。

于 2016-03-31T14:26:52.667 回答
1

只需从 routes.php 文件中剪切所有路由并将其粘贴到中间件组“web”之间,就像这样:

路由文件

于 2016-01-24T12:38:48.253 回答
0

对于 5.2,只需将具有错误变量的路由移动到中间件组

于 2016-02-26T10:27:59.810 回答
-1

使用此代码,您可以捕获错误并显示它们:

@if ($errors->any())
 <div class='alert alert-danger'>
  @foreach ( $errors->all() as $error )
   <p>{{ $error }}</p>
  @endforeach
 </div>
@endif
于 2015-12-26T19:12:47.890 回答