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

  • Do a basic check with `isset`, so the variable is not echoed if not set. – Michael Sørensen Dec 26 '15 at 19:02
  • 1
    @MichaelSørensen It's Laravel, the variable is supposed to always be available in all views. So if it's not set in one view, it's likely a problem that needs to be addressed globally. – Joel Hinz Dec 26 '15 at 19:03
  • @MichaelSørensen i am following the laravel tutorials and there same work has done which that i did but it show's undefined variable $errors and i also tried it with isset($error) check but on validation it does't show any error . – Muhammad Asif Saleem Dec 26 '15 at 19:09

6 Answers6

31

This is a breaking problem with the 5.2 upgrade. What's happening is the middleware which is responsible for making that errors variable available to all your views is not being utilized because it was moved from the global middleware to the web middleware group.

There are two ways to fix this:

  1. In your kernel.php file(app/Http/Kernel.php), you can move the middleware \Illuminate\View\Middleware\ShareErrorsFromSession::class back to the protected $middleware property.

  2. Wrap all your web routes with a route group and apply the web middleware to them:

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

Copied from this post Laravel 5.2 $errors not appearing in Blade

Community
  • 1
  • 1
smartrahat
  • 5,381
  • 6
  • 47
  • 68
  • The same solution with screen shots can be found in this link : http://preprogrammer.com/undefined-variable-errors-in-laravel-5-2-solved/ –  Apr 28 '16 at 04:54
5

Posting this as it might be useful for others,

As Praveen mentioned in 1st solution, in your Kernel.php file(app/Http/Kernel.php) move \Illuminate\View\Middleware\ShareErrorsFromSession::class from $middlewareGroups to protected $middleware property, but the same will start throwing the error "Session store not set on request",

to resolve this move \Illuminate\Session\Middleware\StartSession::class, to $middleware property as well.

Kumar Sambhav Pandey
  • 1,713
  • 4
  • 22
  • 33
2

This happens because the file below is not updated in the composer update process, so doesn't have the mapWebRoutes method implemented.

app/Providers/RouteServiceProvider.php

Copy this file over from a fresh install and it will work. Better, follow the upgrade path on the docs.

DarkWingDuck
  • 2,028
  • 1
  • 22
  • 30
1

Just cut all your routes from routes.php file and paste it between the middleware group 'web', just like this:

Routes file

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
0

For 5.2, simply move the the routes that have the errors variable to middleware group

Robert
  • 5,278
  • 43
  • 65
  • 115
Koko
  • 111
  • 1
  • 7
-1

With this code, you can catch errors and display them :

@if ($errors->any())
 <div class='alert alert-danger'>
  @foreach ( $errors->all() as $error )
   <p>{{ $error }}</p>
  @endforeach
 </div>
@endif
Kokno
  • 185
  • 1
  • 1
  • 8
  • yes we can catch error message from this way but this is not my problem . My problem is that i face such kind of Error : undefined variable $errors.Read the question carefully . – Muhammad Asif Saleem Dec 26 '15 at 19:23
  • 1
    Maybe this can help you : http://stackoverflow.com/questions/34420590/laravel-5-2-validation-errors – Kokno Dec 26 '15 at 19:26
  • the comment of @Kokno should probably be an answer.. the response on the link explains very well what has changed from 5.1 to 5.2 – mikey Apr 18 '16 at 22:14