Pass Controller data to a modal in Laravel view 5.5

0

I'm trying to pass data from a Controller to a Modal in the view.

In the view I have this code that populates a datatable as soon as the page is loaded:

....
@foreach($exames as $exame)
                    <tr>
                        <td>{{$exame->registro}}</td>
                        <td>{{$exame->nome}}</td>
                        <td>{{$exame->motivo}}</td>
                        <td>{{$exame->telefone.' '.$exame->celular.' '.$exame->tel_recado}}</td>
                        <td>{{ Carbon\Carbon::parse($exame->data)->format('d-m-Y') }}</td>
                        <td>
                            <a class="btn btn-xs btn-success" href="{{url('/info_exame',$exame->id)}}">Visualizar</a>
                            <!--<button type="button" class=" btn btn-xs btn-success" data-toggle="modal" data-target="#modal-default-{{$exame->id}}">Visualizar</button>-->
                            <a href="{{url('/edita_exames', $exame->id)}}" class="btn btn-xs btn-info">Editar</a>                               
                            <button type="button" class="btn btn-xs btn-danger" data-toggle="modal" data-target="#modal-apaga-{{$exame->id}}">Apagar</button>
  </td>
 </tr>
@endforeach
....

To popular this datatable I use this route:

Route::get('/agenda_exames', 'AgendaExamesController@index')->name('agenda_exames');

With this Controller:

public function index()
{
    $exames = Tab_exames::all();

    return view('agendaexames', compact('exames'));
}

I was positioning Modal inside the @foreach after the table, but in my understanding it is not a correct way to use it since if the bank has thousands of records, I wonder what size the view file will be.

Well, looking here in the stack I found this example:

Changing the content of Modal Laravel 5.1

I followed the example by creating this Route:

Route::get('/info_exame/{id}', 'AgendaExamesController@show');

With this Controller

public function show($id){

    $info_exame = Tab_exames::findOrFail($id);
    return view('agendaexames', compact('info_exame'));        
}

There are differences between my code and the example code, but I think that would be no problem.

My Modal is this way

@if(isset($info_exame->id))
<!--Modal Mostra Dados Exame-->
<div class="modal modal-success fade" id="modal-default" tabIndex="-1">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <h4 class="modal-title">Informações sobre o agendamento do exame</h4>
        </div>
        <div class="modal-body">
            <div class="row">                                            
                <div class="col-xs-12">
                    <span class="fa fa-calendar"></span>
                    <label>Data do exame</label>
                    <p><b>{{ Carbon\Carbon::parse($info_exame->data)->format('d-m-Y') }}</b></p></p>
                </div>                                                                                  
            </div>
            <hr>
            <div class="row">                                            
                <div class="col-xs-3">
                    <span class="fa fa-arrow-circle-right"></span>
                    <label>Nº Registro</label>
                    <p>{{$info_exame->registro}}</p>
                </div>
                <div class="col-xs-6">
                    <span class="fa fa-arrow-circle-right"></span>
                    <label>Nome</label>
                    <p>{{$info_exame->nome}}</p>
                </div> 
                <div class="col-xs-3">
                    <span class="fa fa-wheelchair"></span>
                    <label>Deficiência</label>
                    <p>{{$info_exame->deficiencia}}</p>
                </div> 
            </div>
            <div class="row">                                             
                <div class="col-xs-6">
                    <span class="fa fa-arrow-circle-right"></span>
                    <label>Motivo</label>
                    <p>{{$info_exame->motivo}}</p>
                </div>
                <div class="col-xs-3">
                    <span class="fa fa-phone"></span>
                    <label>Telefone</label>
                    <p>{{$info_exame->telefone}}</p>
                </div>
                <div class="col-xs-3">
                    <span class="fa fa-phone"></span>
                    <label>Celular</label>
                    <p>{{$info_exame->celular}}</p>
                </div>
            </div>
            <div class="row">
                <div class="col-xs-3">
                    <span class="fa fa-phone"></span>
                    <label>Recado</label>
                    <p>{{$info_exame->tel_recado}}</p>
                </div>
                <div class="col-xs-3">
                    <span class="fa fa-phone"></span>
                    <label>Nome do contato</label>
                    <p>{{$info_exame->nome_contato}}</p>
                </div>
            </div>
            <div class="row">
                <div class="col-xs-12">
                    <span class="fa fa-info-circle"></span>
                    <label>Observações</label>
                    <p>{{$info_exame->observacoes}}</p>
                </div>
            </div>
            <hr>

            <div class="row"> 

                <div class="col-xs-4">
                    <span class="fa fa-user-circle"></span>
                    <label>Cadastrado por</label>
                    <p>{{$info_exame->operador}}</p>
                </div>

                <div class="col-xs-4">
                    <span class="fa fa fa-calendar"></span>
                    <label>Gravado em</label>
                    <p>{{ Carbon\Carbon::parse($info_exame->created_at)->format('d-m-Y - H:i:s') }}</p>
                </div>
                <div class="col-xs-4">
                    <span class="fa fa fa-calendar"></span>
                    <label>Atualizado em</label>
                    <p>{{ Carbon\Carbon::parse($info_exame->update_at)->format('d-m-Y - H:i:s') }}</p>
                </div>
            </div>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default pull-right" data-dismiss="modal">Fechar</button>
        </div>                                    
    </div>
    <!-- /.modal-content -->
</div>
<script type="text/javascript">
    $(window).load(function(){
        $('#modal-default').modal('show');
    });
</script>
<!-- /.modal-dialog -->

When I click on the table button I have this error

What could be wrong? Is there another more correct and objective way of passing controller data to a modal?

    
asked by anonymous 29.01.2018 / 00:17

0 answers