How to load modal edit dynamically using Laravel?

2

In an application I'm doing the following to load my Bootstrap Modals

index.blade.php

obs: data-target (#edit {{$ ecategory-> id}}) used to call modal

@foreach($ecategories as $ecategory) //listando os registros
  <tr class="danger">
    <td>{{ $ecategory->name }}</td>
    <td> <a href="{{route('categories.edit', ['id'=>$ecategory->id])}}" data-toggle="modal" data-target="#edit{{$ecategory->id}}" class="btn btn-default btn-sm" >Editar</a>
    <a href="{{route('categories.destroy', ['id'=>$ecategory->id])}}"  class="btn btn-danger btn-sm" >Excluir</a> </td>
  </tr>
    @endforeach

@foreach($ecategories as $ecategory) // listando os modals de cada registro
<div class="modal fade" id="edit{{$ecategory->id}}" tabindex="-1" role="dialog" aria-labelledby="categoryedit_title" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content"></div>
  </div>
</div>
  @endforeach

edit.blade.php

<div class="modal-header">
  <button type="button" class="close" data-dismiss="modal">&times;</button>
  <h4 id='categoryedit_title' class="modal-title">Editando Categoria</h4>
</div>
<div class="modal-body">
  @include('errors._check')

  {!! Form::model($category, ['route'=>['categories.update', $category->id]])  !!}

  @include('categories._form')

  <div class="form-group" >

    {!! Form::submit('Atualizar Categoria', ['class'=>'btn btn-primary']) !!}
  </div>

  {!! Form::close()  !!}


  </div>

  <div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>

  </div>

So I was able to open the modal of each record, but then my html gets full of modals

<div class="modal fade" id="edit2" tabindex="-1" role="dialog" aria-labelledby="categoryedit_title" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content"></div>
  </div>
</div>
    <div class="modal fade" id="edit5" tabindex="-1" role="dialog" aria-labelledby="categoryedit_title" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content"></div>
  </div>
</div>
    <div class="modal fade" id="edit6" tabindex="-1" role="dialog" aria-labelledby="categoryedit_title" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content"></div>
  </div>
</div>
    <div class="modal fade" id="edit7" tabindex="-1" role="dialog" aria-labelledby="categoryedit_title" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content"></div>
  </div>
</div>
    <div class="modal fade" id="edit8" tabindex="-1" role="dialog" aria-labelledby="categoryedit_title" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content"></div>
  </div>
</div>

I'm trying to dynamically generate, but I was not successful. does anyone have a solution?

    
asked by anonymous 26.09.2017 / 20:52

1 answer

1

Your problem is occurring here:

@foreach($ecategories as $ecategory) // listando os modals de cada registro
<div class="modal fade" id="edit{{$ecategory->id}}" tabindex="-1" role="dialog" aria-labelledby="categoryedit_title" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content"></div>
  </div>
</div>
  @endforeach

As you are looping it is creating several modalities there when you click the button to open the modal in specific it ends up taking the wrong modal reference.

    
26.09.2017 / 21:25