How to use "echo" in an Laravel view?

Asked

Viewed 674 times

3

I’m hoping that in mine layout blade echo only if the variable $errors is defined:

@section('mensagens')
    <div class="container"> 
      <div class="row"> 
        <div class="col-md-2"></div>
        {{ isset($errors) ? 
            '<div class="col-md-8 alert alert-danger">'
              $errors->first('prontuario')
              $errors->first('senha')
            '</div>'
            :
            ''
        }}

        <div class="col-md-2"></div>
      </div>
    </div>

    @show

How do I get him to just show this div class alert-danger if the variable $errors of my form is defined?

2 answers

3


You can also do so:

@section('mensagens')
<div class="container"> 
  <div class="row"> 
    <div class="col-md-2"></div>
        @if (count($errors))
            <div class="col-md-8 alert alert-danger">
                @foreach ($errors->all() as $error)
                    {{ $error }}
                @endforeach
            </div>
        @endif
    <div class="col-md-2"></div>
  </div>
</div>
@show

Instead of you set each individual error, you go through all the errors and display each one.

  • It worked!!! It was exactly the way I wanted it, only showing up the div when I made the mistake. Thanks!

  • Not at all! We’re here to help each other! ;)

0

Use @if and @endif

@section('mensagens')
    <div class="container"> 
      <div class="row"> 
      @if($errors)
          <div class="col-md-2"></div>
              <div class="col-md-8 alert alert-danger">
                  {{ $errors->first('prontuario') }}
                  {{ $errors->first('senha') }}
              </div>
              <div class="col-md-2"></div>
          </div>
      @endif
    </div>
@show
  • Thanks for the answer. But even if the variable $errors is not defined, the small rectangle of the div still appears :/

  • Goes up the IF...

  • Not going. DIV Alert still shows up. I’m thinking about doing this part in PHP...

  • Put your Controller where this variable is.

  • if ($Validator->fails()) { Return Redirect::to('/') ->withErrors($Validator) // sends back all errors to the form ->withInput(Input::except('password')); }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.