skip to Main Content

From a request handler I pass a parameter to a request view:

public function sol_solicitante(Request $request, $param){
    return view('solicitante.solicitudes', compact('param'));
}

The view that the controller invokes is:

@extends('layouts.app')

@section('title','Buzón Solicitudes')

@section('css')
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.2/css/bootstrap.css">
    <link rel="stylesheet" href="https://cdn.datatables.net/1.10.22/css/dataTables.bootstrap4.min.css">
    <link rel="stylesheet" href="https://cdn.datatables.net/responsive/2.2.6/css/responsive.bootstrap4.min.css">
@endsection

@section('content')
    <div class="container mt-4">  
        <div>
            <h2>Buzón De Solicitudes</h2>  
        </div>
        <div class="card">
            <div class="card-body">
                
                <!--
                    <a href="crear">
                        <button type="button" class="btn btn-success float-right">Crear Solicitud</button>
                    </a>
                </h2>
                -->
                <table class="table table-hover" id="buzonsolicitudes">
                    <thead>
                        <tr>
                            <th scope="col">Id</th>
                            <th scope="col">Nombre</th>
                            <th scope="col">Estado</th>
                            <th scope="col">&nbsp;</th>
                        </tr>
                    </thead>                    
                </table>            
            </div>
        </div>
    </div>
@endsection

@section('js')
    
    <script src="https://code.jquery.com/jquery-3.5.1.js"></script>    
    <script src="https://cdn.datatables.net/1.10.22/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.22/js/dataTables.bootstrap4.min.js"></script>
    <script src="https://cdn.datatables.net/responsive/2.2.6/js/dataTables.responsive.min.js"></script>
    <script src="https://cdn.datatables.net/responsive/2.2.6/js/responsive.bootstrap4.min.js"></script>
    <script>   
    
        $('#buzonsolicitudes').DataTable({
            "ajax": "{{ route('datatable.sol_solicitante',['param' => '1'])}}",
            "columns":[
                {data:'id'},
                {data:'nombre'},
                {data:'estadologico'},
                {data:'btn'}
            ],
            responsive: true,
            autoWidth: false,
            "language": {
                "lengthMenu": "Mostrar " + 
                                `<select class="custom-select" custom-select-sm form-control form-control-sm>
                                    <option value='10'>10</option>
                                    <option value='25'>25</option>
                                    <option value='50'>50</option>
                                    <option value='100'>100</option>
                                    <option value='-1'>Todos</option>
                                </select>` +
                                " registros por página",            
                "zeroRecords": "Nada encontrado - disculpa",
                "info": "Mostrando página _PAGE_ de _PAGES_",
                "infoEmpty": "Ningún registro disponible",
                "infoFiltered": "(filtrado de _MAX_ registros totales)",
                'search':'Buscar',
                'paginate':{
                    'next':'siguiente',
                    'previous':'anterior'
                }
            }
        });
    </script>
@endsection

What I don’t know is how to access, in the view, the parameter sent by the controller, since I must pass it as a parameter to the Ajax call; Something like:

<script>   
    var p= param

    $('#buzonsolicitudes').DataTable({
        "ajax": "{{ route('datatable.sol_solicitante',['param' => p])}}",

This that I put does not work and checking I do not find how it should be.

I appreciate all help, thank you.

3

Answers


  1. You should create a script in blade file. In that script, you add init some variables with value from controller

    Login or Signup to reply.
  2. php codes are processed on server. the blade compiles all variables before page loads. look you are already using route helper. just pass the variable to the helper as you do in other places.

    "ajax": "{{ route('datatable.sol_solicitante', ['param' => $param]) }}"
    

    this will generate the route as you defined on your web.php file like mysite.com/datatable/sol_solicitante/param_value

    Login or Signup to reply.
  3. Add this in your script

     var data ={!! $data->id !!};  //data->id is variable send by controller
    

    you can directly access this variable in script

    Try this hope it works

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search