Search code examples
laraveldatatabledatatableslaravel-8yajra-datatable

How to pass user_id in ajax route datatable Laravel


I have a datatable in Laravel, here is my code

$(function() {
        var user_id = $(this).data('user_id');
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

        var table = $('.data-table').DataTable({
            paging: true,
            info: true,
            autoWidth: false,
            responsive: true,       
            processing: true,
            serverSide: true,
            ajax: "{{ route('detail.user', 'user_id')}}",
         
            columns: [{
                    data: 'DT_RowIndex',
                    name: 'DT_RowIndex',
                    orderable: false,
                    searchable: false,
                },
                {
                    data: 'title',
                    name: 'title',
                    orderable: false,
                },
                {
                    data: 'content',
                    name: 'content',
                    orderable: false,
                    visible: false,
                },
                {
                    data: 'progress',
                    name: 'progress'
                },
                {
                    data: 'status',
                    name: 'status'
                },
                {
                    data: 'kpi',
                    name: 'kpi'
                },
                {
                    data: 'target_selesai',
                    name: 'target_selesai'
                },
                {
                    data: 'action',
                    name: 'action',
                    orderable: false,
                    searchable: false
                },
            ]
        });

        $('body').on('click', '.detailProduct', function() {
           ...
        });
        $('#saveBtn').click(function(e) {
           ...
        });

        $('body').on('click', '.deleteProduct', function() {
           ...
        });

    });

I want to get user_id value from url parameter (ex: http://127.0.0.1:8000/detail/1000000004 ), I've tried code above, but it will return empty value. Seems like I write wrong code to get url parameter. Because, If I change the ajax: "{{ route('detail.user', 'user_id')}}", into ajax: "{{ route('detail.user', '1000000004')}}", the program will works as expected. What sould I do? thanks in advance


Solution

  • You can not set variable in PHP code using JavaScript. You should not use PHP code on dynamic JavaScript code. There are 2 alternatives :

    1. Use static path instead of Laravel route, just change ajax: "{{ route('detail.user', 'user_id')}}" to ajax: "/detail/"+user_id

    2. Send user_id variable using Laravel blade view arg. on your controller define user_id like below:

      return view('index',
              ['user_id', 1000000004]
          );
      

      then call it on the blade as php variable ($user_id) example ajax: "{{ route('detail.user', $user_id)}}"