0

I have a datatable view that shows some post from user, I want to create a datatable with a button that can show all user post. But, when moving from all post lists to user post lists I cant get user_id from the URL. I want to get the user_id using $request->route. Here is my controller:-

 public function index(Request $request)
    {
          if ($request->ajax()) {
            $request->route('detail');
            $data = Post::where('user_id', $this->route('detail'))->latest()->get();
            return Datatables::of($data)
                ->addIndexColumn()
                ->addColumn('action', function ($row) {

                    $btn = ' <a href="javascript:void(0)" data-toggle="tooltip"  data-id="' . $row->id . '" data-original-title="Detail" class="btn btn-success mr-1 btn-sm detailProduct"><span class="fas fa-info"></span></a>';

                    return $btn;
                })
                ->rawColumns(['action'])
                ->make(true);
        }

        return view('detail');
    }

Here is my route:-

Route::resource('detail', DetailController::class);

datatable code

var table = $('.data-table').DataTable({
            paging: true,
            info: true,
            autoWidth: false,
            responsive: true,
            
            columnDefs: [{
                    "targets": [0, 2, 3, 4, 5, 6], // your case first column
                    "className": "text-center",

                },

            ],
            language: {
                paginate: {
                    next: '<span class="fas fa-arrow-right"></span>', // or '→'
                    previous: '<span class="fas fa-arrow-left"></span>' // or '←' 
                }
            },
            oLanguage: {
                "oPaginate": {
                    next: '&#8594;', // or '→'
                    previous: '&#8592;' // or '←' 
                },
                "sLengthMenu": "Tampilkan _MENU_ Item",
                "sEmptyTable": "Tidak ada data",
                "sInfoEmpty": "Tidak ditemukan",
                "sLoadingRecords": "Sedang memproses - loading...",
                "sInfo": "Menampilkan _START_ - _END_ dari _TOTAL_ Item",
                "sSearch": "Cari:",
            },
            processing: true,
            serverSide: true,
            ajax: "{{ route('detail.index') }}",

            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: 'target_selesai',
                    name: 'target_selesai'
                },
                {
                    data: 'action',
                    name: 'action',
                    orderable: false,
                    searchable: false
                },
            ]
        });

If I change the $this->route('detail') into user_id value, the program will work as expected. Is there something wrong with my code? thanks in advance

Vysco Zyza
  • 109
  • 1
  • 12

1 Answers1

0

Change this:-

$this->route('detail')

To:-

$request->get('details')

And your ajax method should be like this:

"ajax": {
    'type': 'POST',
    'url': '{{ route('detail.index') }}',
    'data': {
       detilas: 'your id',
      
    },
}

your roure:

Route::post('detail', DetailController::class);
Zia Yamin
  • 942
  • 2
  • 10
  • 34
  • The error dissapear, but datatable not showing any data. Where I can see my ajax code? – Vysco Zyza Feb 13 '21 at 04:28
  • as you checked if ``if ($request->ajax())``, so you have ajax code, it is in your blade file – Zia Yamin Feb 13 '21 at 04:31
  • ```details: 'your id',``` what is 'your id'? is that a data or variable name? – Vysco Zyza Feb 13 '21 at 04:59
  • yes, an `id` that you send to the `controller`, and by that, you check your ``where`` condition – Zia Yamin Feb 13 '21 at 05:00
  • I have try your suggestion, if I add the ```route::match``` it will return laravel error said ```App\Http\Controllers\DetailController` is not invokable.``` if I not add that route match code the datatable still not showing any data – Vysco Zyza Feb 13 '21 at 05:09
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/228661/discussion-between-zia-yamin-and-vysco-zyza). – Zia Yamin Feb 13 '21 at 05:41