How do the modal pick up foreach values with Laravel?

Asked

Viewed 28 times

-2

I need to make the modal show the value of the item that is selected in the foreach, however it is only taking the last value. Follows the code:

<div class="row justify-content-center">
    @csrf
    <!--Listagem-->
    <h2>Tipos de Produtos</h2>
    <button type="button" class="btn btn-primary create-tipoproduto">Adicionar</button>
    <table class="table table-hover">
        <thead>
            <tr>
                <th class="col-sm-1">ID</th>
                <th class="col-sm-6">Descrição</th>
                <th class="col-sm-1">Inflável</th>
                <th class="col-sm-2">Ações</th>
            </tr>
        </thead>
        <tbody>
            @foreach ($tiposprodutos as $tipoproduto)
            <tr>
                <td>{{ $tipoproduto->id }}</td>
                <td>{{ $tipoproduto->descricao }}</td>
                <td>{{ $tipoproduto->inflavel }}</td>
                <td><button type="button" class="btn btn-primary remove-tipoproduto"></button></td>
            </tr>
            @endforeach
        </tbody>
    </table>
    <!--Dialog Excluir-->
    <div class="modal fade" id="dialog-form-excluir" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Excluir Tipo de Produto</h5>
                </div>

                <form id="form-excluir" action="{{  route('excluir_tipoproduto', ['id' => $tipoproduto->id]) }}" method="POST">
                    <div class="modal-body">
                        @csrf
                            <fieldset>
                                <label for="desc">Descrição</label>
                                <input class="form-control text-input" type="text" name="descricao" id="descricao" value="<?php echo $tipoproduto['descricao']?>" disabled>
                                <label for="infl">Inflável?</label>
                                <input class="form-control text-input" type="text" name="inflavel" id="inflavel" value=<?php echo $tipoproduto['inflavel']?> disabled>
                            </fieldset>
                      </div>
                      <div class="modal-fade">
                          <button class="btn btn-primary" type="submit" id="botaoExcluir" name="botaoExcluir">Excluir</button>
                      </div>
                </form>
          </div>
    </div>
</div>

            

  • You will have to work with Javascript, the click button on the product line should call a JS function that passes the product data to the modal screen and configure the confirm delete button, which should call a server-side script, passing the product code

1 answer

0

You can recover the correct data by performing a request with ajax to the backend and configure the modal inputs from the return of it.

With the attribute onclick on the tag button, when clicked, it will call the function that will configure the inputs at the same time that opens the modal.

Browser other questions tagged

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