How to load edit modal dynamically using Laravel?

Asked

Viewed 1,512 times

2

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

index.blade.php

Obs: data-target(#Edit{$ecategory->id}}) used to call modals

@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 managed 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 generate dynamically, but I was unsuccessful. someone has a solution?

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 making a loop he is creating several modals there when you click on the button to open the modal in specific he ends up picking the wrong modal reference.

  • 1

    This way what I’m doing is working, it’s not getting any wrong information, what I want is to take this loop, and load the modal dynamically, but take advantage of Edit.blade.php and the route

  • In this case you can do an if in Blade for example: @if(Rout::getCurrentRouteName() == "Rotax") HERE IS WHAT YOU WANT TO SHOW @endif

  • 1

    not understood very good, but is giving error Undefined offset: 0

  • Another example: @if(Route::currentRouteName() == "lancings.index" ) @include('layout.header-with-blue-menu') @end Another example, I use this to modify the aesthetic structure of menus

  • That doesn’t solve my case.

Browser other questions tagged

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