Home » Php » php – How to call a modal in a function using Laravel?

php – How to call a modal in a function using Laravel?

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have the next modal:

<div class="modal fade" id="mostrarmodal" tabindex="-1" role="dialog" aria-labelledby="basicModal" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
       <div class="modal-header">
          <h3><center>Mensaje del Sistema</center></h3>
       </div>
       <div class="modal-body">
          <h4><center>¡Aviso!</center></h4>
          <center>¡Cliente no encontrado!</center>
      </div>
       <div class="modal-footer">
      <a href="#" data-dismiss="modal" class="btn btn-info">Continuar</a>
       </div>
  </div>

I would like to call this modal in the “else” of the function…
I put a comment in the function indicating where I would like to call the modal

This is my function:

public function resultados_clientes(Request $request){
  $busqueda = $request->cliente;
  $clientes = strtoupper($busqueda);
  $perimetro = DB::table('tbl_perimetros as a')
    ->select('a.id','a.rif','a.cod_cliente','a.razon_social','a.holdings_id')
    ->leftjoin('tbl_holdings as b','b.id','=','a.holdings_id')
    ->select(array('a.id','b.hrif','b.hrazon_social','a.rif','a.razon_social','a.estatus'))
    ->where('a.rif','like', '%' . $clientes . '%')
    ->orWhere('a.razon_social', 'like', '%' . $clientes . '%')
    ->orWhere('b.hrif', 'like', '%' . $clientes . '%')
    ->orWhere('b.hrazon_social', 'like', '%' . $clientes . '%')  
    ->get();

  if($perimetro->count()) {

    return view('Busqueda_est.resultados_clientes',compact('perimetro','busqueda'));
  }
    //HERE IS THE MODAL

    return view('Busqueda_est.index');
}

How could I do that?

How to&Answers:

You can try chaining view() functions to each other, like this:

//HERE IS THE MODAL

// create separate view file for your modal
return view('Busqueda_est.index').view('modalView');

Answer:

I found the solution:

public function resultados_clientes(Request $request){
  $busqueda = $request->cliente;
  $clientes = strtoupper($busqueda);
  $perimetro = DB::table('tbl_perimetros as a')
    ->select('a.id','a.rif','a.cod_cliente','a.razon_social','a.holdings_id')
    ->leftjoin('tbl_holdings as b','b.id','=','a.holdings_id')
    ->select(array('a.id','b.hrif','b.hrazon_social','a.rif','a.razon_social','a.estatus'))
    ->where('a.rif','like', '%' . $clientes . '%')
    ->orWhere('a.razon_social', 'like', '%' . $clientes . '%')
    ->orWhere('b.hrif', 'like', '%' . $clientes . '%')
    ->orWhere('b.hrazon_social', 'like', '%' . $clientes . '%')  
    ->get();

  if($perimetro->count()) {
    return view('Busqueda_est.resultados_clientes',compact('perimetro','busqueda'));
  }

  $error_code = 1;
  return view('Busqueda_est.index', compact('error_code'));

}

Create a variable $error_code for call in the function of the controller

then call a script in the html like this:

@if(!empty($error_code) && $error_code == 1)
<script>
$(function() {
$('#mostrarmodal').modal('show');
});
</script>
@endif

The modal have to be in the html to be called.