Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel MessageBag errors array is empty in view but with content if I kill script

I am trying to return errors back to my view, this is part of my controller TestcategoryController

    $rules =array(
        'name' => 'required'
    );
    $validator = Validator::make(Input::all(), $rules);
    //process
    if($validator->fails()){
        return Redirect::to('testcategory/create')->withErrors($validator);
    }

In the view testcategory/create if I try and output the errors like

        @if($errors->any())         
            {{ $errors->first('name') }}
        @endif

I get nothing. But if I {{dd($errors)}} I get

      object(Illuminate\Support\ViewErrorBag)#91 (1) { ["bags":protected]=> array(1) { 
      ["default"]=> object(Illuminate\Support\MessageBag)#92 (2) 
      { ["messages":protected]=>   array(1) 
      { ["name"]=> array(1) { [0]=> string(27) "The name field is required." } }  
      ["format":protected]=> string(8) ":message" } } }

The only way I am getting the errors is if I kill the script. What am I doing wrong?

like image 372
briankip Avatar asked Dec 05 '25 13:12

briankip


1 Answers

Probably another issue would be with $errors not being saved to Session variable $errors and nothing being shown in the view.

Here is an example of the same issue: http://laravel.io/forum/03-28-2016-errors-variable-empty-after-failed-validation

For me the solution defined in the above link worked. Solution: Is in app\Http\Kernel.php Move \Illuminate\Session\Middleware\StartSession::class, from $middlewareGroups to $middleware

Before

enter image description here

After

enter image description here

like image 180
Manjunath Reddy Avatar answered Dec 07 '25 15:12

Manjunath Reddy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!