2
votes

I have problem with Laravel as title say. My code:

Function in controller:

$validator = Validator::make($request->all(), [
        'login' => 'required|max:10',
        'katalog' => 'required',
        'limitip' => 'required|ip',
    ]);

    if($validator->fails()){
        return redirect('something/toedit/someone')->withErrors($validator)->withInput();
    }
    else{
        echo "Clear.";
    }

And route:

Route::get('something/toedit/{login}',['middleware' => 'auth', 'uses'=>'MyController@editAccountGet']);

Route::post('something/toedit',['middleware' => 'auth', 'uses'=>'MyController@editAccountPost']);

And now problem is that this code in my view always return nothing.

@if (count($errors) > 0)
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

Also {{count($errors)}} it's equal 0. But if i use code like this:

@if (Session::has('errors'))
    {{var_dump(Session::get('errors'))}}
@endif

It's returning in my view that:

object(Illuminate\Support\ViewErrorBag)#146 (1) { ["bags":protected]=> array(1) { ["default"]=> object(Illuminate\Support\MessageBag)#147 (2) { ["messages":protected]=> array(1) { ["login"]=> array(1) { [0]=> string(48) "The login may not be greater than 10 characters." } } ["format":protected]=> string(8) ":message" } } }

Can someone help me to access errors from $errors variable in my blade view? I'm really confused here.

2
Try session() - >get('errors') - Abhishek
Why don't you just try it once? - Abhishek
->withFoo('bar') bar will be added as session flash in foo key. - Abhishek

2 Answers

1
votes

I had the similar issue on Laravel 5.2.41. In my case, $errors->has() always return 1, even if there is no error.

Found the solution from Laracasts:-

$errors->has() returns true since framework upgrade

Replace $errors->has() with $errors->any() or $errors->count() > 0 will do.

0
votes

The $errors variable can be added by the web middleware group, which includes ShareErrorsFromSession. You can apply this to your routes in a few different ways:

// Apply to a single route
Route::get('/', ['middleware' => ['auth', 'web'], function () {
    //
}]);

// To a group of routes
Route::group(['middleware' => ['auth', 'web']], function () {
    Route::get(...);
    Route::post(...);
});

See https://laravel.com/docs/5.2/middleware#assigning-middleware-to-routes