How to use forelse without error in form - LARAVEL?

1

Good afternoon, I have a form that it brings an array of the database through user_id, but it has a problem after I put the forelse it is not bringing all the data, because if it has more than one data it duplicates the form.

I do not know if I put the div in the wrong place, it follows the code below, At first I just wanted to pass a message if I did not have a user-related record.

@extends ('layouts.app') @section ('content')

<div class="container">
<div class="row">

    <form class="form-horizontal " id="regForm" action="{{route('renovacao.store')}}" method="POST">
        <div class="card-panel white">
            <h4 class="center">Solicitar Renovação</h4>
            <div class="row"></div>
            {{ csrf_field()}}

            <div class="row">
                @forelse($matricula as $matric)
                <div class="row">
                        <right>
                         <a>**Dados Cadastrados**</a>
                    </right>
                    <div class="row"></div>
                    <div class="row"></div>
                    <div class="col s6 m6">
                    <div class="input-field {{$errors->has('user_id') ? 'has-error' : ''}} ">
                        <label for="produto">Nome do Pai:</label>
                       <input type="text" class="form-control" name="rematricula[{{ $loop->index}}][nomerespo]" value="{{ $matric->nomedopai }}">

                    </div>
                    </div>

                    <div class="col s6 m6">
                    <div class="input-field {{$errors->has('user_id') ? 'has-error' : ''}} ">
                        <label for="produto">Nome do Aluno(a):</label>
                        <input type="text" class="form-control" name="rematricula[{{ $loop->index}}][nomealuno]" value="{{ $matric->nomealuno }}">

                    </div>
                    </div>

                </div>

                 <div class = "row">
                    <div class="col s12">

                        <a title="Voltar Para Página Principal" class="btn orange darken-4 btn-info left " href="/admin">Voltar
                            <i class="material-icons left">arrow_back_ios</i>
                        </a>

                        <button type="submit" class="btn orange darken-4 btn-info right">Confirmar
                            <i class="material-icons left">save</i>
                        </button>
                    </div>
                </div>

                @empty
                        <p>Não existe registro</p>
              @endforelse


            </div>
        </div>    
    </form>

</div>

@endsection

- Controller that sends the data to form

public function listardados(){
    $matricula = Matricula::where('user_id', Auth::id())->get();

    //dd($matricula);
    return view('dashboard.renovacao.teste', compact( 'matricula'));
}
    
asked by anonymous 29.11.2018 / 17:56

1 answer

1

The @forelse is equal to @foreach , the difference is that this directive has a treatment for empty arrays .

Example with @foreach

@section('content')
    @if($matriculas->count())
        @foreach($matriculas as $matric)
            <input type="text" value="{{ $matric->donomepai }}">
        @endforeach
    @else
        <p>Não possui matriculas.</p>
    @endif
@endsection

Example with @forelse

@section('content')
    @forelse($matricula as $matric)
        <input type="text" value="{{ $matric->nomedopai }}">
    @empty
        <p>Não possui matrículas.</p>
    @endforelse
@endsection

Blade Templates - Loops

    
29.11.2018 / 18:32