Quaap's avatar

Undefined variable: $errors

Trying to learn Laravel, I just installed the latest version of Laravel. I'm relatively new to Laravel , and only tried using Laravel 4 for a very short time. But in version 4 I had no problems with outputting errors to a view, now in version 5.2 I'm getting :

Undefined variable: errors (View: /resources/views/home.blade.php)

I searched the web for a solution, and found that I had to change the kernel.php file...

from:

protected $middleware = [
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
];

 /**
 * The application's route middleware groups.
 *
 * @var array
 */
protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
    ],

    'api' => [
        'throttle:60,1',
    ],
];

To:

protected $middleware = [
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    \App\Http\Middleware\EncryptCookies::class,
    \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
    \Illuminate\Session\Middleware\StartSession::class,
    \Illuminate\View\Middleware\ShareErrorsFromSession::class,
    \App\Http\Middleware\VerifyCsrfToken::class,
];

 /**
 * The application's route middleware groups.
 *
 * @var array
 */
protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
    ],

    'api' => [
        'throttle:60,1',
    ],
];

In my routes file I moved all routes within the "web" middleware group, as advised on several forums. But I still get an error, only a different one:

RuntimeException in Request.php line 852: Session store not set on request.

I'm not sure but it can't be the intention of the Laravel developers, that I have to modify core files to get my project working. So I must be doing something wrong. Does anyone have any idea how to solve this?

0 likes
4 replies
Gerard's avatar

Seems that you are trying to use a variable called errors in your view but you aren't passing the variable from the corresponding controller.

HomiWong's avatar

If you don't want to change Kernel.php , always place your routes in web middleware

Route::group(['middleware' => ['web']], function () {
    // if you want the $errors , place your routes here
});

By default , $errors come from \Illuminate\View\Middleware\ShareErrorsFromSession::class class, and \Illuminate\View\Middleware\ShareErrorsFromSession::class needs session , so we also need \Illuminate\Session\Middleware\StartSession::class . This is why your solution works.....

Quaap's avatar

Thanks @Gerard

As I mentioned I'm new tot Laravel, so I'm following some tutorials to get the hang of it ;-)

This is my Controller file:

<?php

namespace App\Http\Controllers;

use \Illuminate\Http\Request;

class UserActionController extends Controller 
{
   public function getUserAction($action , $name = null)
   {
      return view('actions.' . $action, ['name' => $name]);
   }
   
   public function createUserAction(Request $request)
   {
      $this->validate($request, [
         'action' => 'required',
         'name' => 'required|alpha'
      ]);  
      return view('actions.create', ['action' => $request['action'], 'name' => $request['name']]); 
   }  
}

In Laravel 4 I added something like ->withErrors($errors); to the view. The tutorial I'm following, as the above code is showing, should work without it.

My routes file:

Route::group(['middleware' => ['web']], function () {
    
    Route::get('/', function () {
        return view('home');
    })->name('home');
    
    Route::group(['prefix' => 'do'], function() {
   
        Route::get('/{action}/{name?}', [
            'uses' => 'UserActionController@getUserAction',
            'as' => 'get_action'
        ]);
        
        Route::post('/', [
            'uses' => 'UserActionController@createUserAction',
            'as' => 'create_action'
        ]);
        
    });
    
});
Quaap's avatar

I started all over with another test project. I moved my routes, in the route file, to web middleware, as suggested. Now the

Undefined variable: errors (View: /resources/views/home.blade.php)

is gone, but still no errors are send to the view, when validation fails in my controller.

<?php

namespace App\Http\Controllers;

use App\Quote;
use App\Author;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class QuoteController extends Controller
{
   public function getIndex()
   {
      $quotes = Quote::all();
      
      return view('index', ['quotes' => $quotes]);
   } 
}
public function postQuote(Request $request)
{
      $this->validate($request, [
         'author' => 'required|max:60|alpha',
         'quote' => 'required|max:500'
      ]);

      $quote = new Quote();
      $quote->quote = $quoteText;
      $author->quotes()->save($quote);
     
      return redirect()->route('index')->with([
         'succes' => 'Quotes saved!'
      ]);
   }
}

The Laravel documentation says:

The validate method accepts an incoming HTTP request and a set of validation rules. If the validation rules pass, your code will keep executing normally; however, if validation fails, an exception will be thrown and the proper error response will automatically be sent back to the user. In the case of a traditional HTTP request, a redirect response will be generated.

The way I read this is, that I should not have to pass the variable, called errors, from the corresponding controller to the view myself .

Could the problem be that I'm on a secure server, and using https?

Anyone a suggestion?

Please or to participate in this conversation.