Displaying the Error Messages in Laravel after being Redirected from controller

后端 未结 9 1140
情书的邮戳
情书的邮戳 2020-12-13 01:44

How can I display the validation message in the view that is being redirected in Laravel ?

Here is my function in a Controller

publi         


        
9条回答
  •  伪装坚强ぢ
    2020-12-13 02:18

    Laravel 4

    When the validation fails return back with the validation errors.

    if($validator->fails()) {
        return Redirect::back()->withErrors($validator);
    }
    

    You can catch the error on your view using

    @if($errors->any())
        {{ implode('', $errors->all('
    :message
    ')) }} @endif

    UPDATE

    To display error under each field you can do like this.

    
    @if($errors->has('firstname'))
        
    {{ $errors->first('firstname') }}
    @endif

    For better display style with css.

    You can refer to the docs here.

    UPDATE 2

    To display all errors at once

    @if($errors->any())
        {!! implode('', $errors->all('
    :message
    ')) !!} @endif

    To display error under each field.

    @error('firstname')
        
    {{ $message }}
    @enderror

提交回复
热议问题