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

后端 未结 5 491
太阳男子
太阳男子 2020-12-21 13:32

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

    $rules =array(
        \'name\' => \'required\'
            


        
相关标签:
5条回答
  • 2020-12-21 13:51

    You need to use it like this:

    {{ $errors->getBag('default')->first('name') }}
    
    0 讨论(0)
  • 2020-12-21 14:04

    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

    After

    0 讨论(0)
  • 2020-12-21 14:12
        Route::group(['middleware' => ['web']], function() {
        Route::get('/home', 'HomeController@index')->name('home');
        });
    

    Add middleware, and resolved..

    0 讨论(0)
  • 2020-12-21 14:13

    If you get nothing in your case, than it means you have overridden your $errors object with something else or with empty object - check your code and data you pass to the views. Your code is perfectly valid and works good - I've tested it locally. Maybe, you passed empty $errors object to your subview, but in other views the correct object is used.

    0 讨论(0)
  • 2020-12-21 14:14

    I was having the same problem with Laravel 8.0

    The only way I could solve this was by entering the error bags layer by layer until I got to the messages manually:

    @if (count($errors->getBags()))
        <div class="alert alert-danger">
            <ul>
                @foreach ($errors->getBags() as $bag)
                    @foreach ($bag->getMessages() as $messages)
                        @foreach ($messages as $message)
                            <li>{{ $message }}</li>
                        @endforeach
                    @endforeach
                @endforeach
            </ul>
        </div>
    @endif
    
    0 讨论(0)
提交回复
热议问题