laravelyajra-datatable

Is there any way to pass the parameter value to Yajra datatable? Please see the code below


//Controller

public function admin_dashboard(CollectionDataTable $dataTable1)
{
       $from = $to = date('Y-m-d');

       $dataTable = $dataTable1->with('from',$from)->with('to',$to)->html();
       return view('operation.admin_dashboard',compact('from','to', 'dataTable'));
}

public function get_collection_table(CollectionDataTable $dataTable,Request $request)
{
    //$from = $to = date('Y-m-d');
    return $dataTable->with($request->all())->render('operation.admin_dashboard');
}

//In route

Route::get('/ahsdashdgasdhadgas', 'Operation\BillingDepartmentController@get_collection_table')->name('ahsdashdgasdhadgas');

//Yajra Datatable

public function html()
{
    return $this->builder()
        ->addTableClass('table table-bordered table-secondary table-hover')
        ->setTableId('collection-table')
        ->columns($this->getColumns())
        ->postAjax([
            'url' => route('ahsdashdgasdhadgas'),
            //'type' => 'get',
        ])

}

I can not access the parameter in datatable. Anyone help me to pass parameter in the datatable route so that I can use the parameter for query?


Solution

  • How do I pass the parameter in the Yajra data table??

    You could make use of the data option

    $builder->postAjax([
        'url' => route('users.index'),
        'data' => 'function(d) { d.key = "value"; }',
    ])
    

    Documentation


    Side note: You are using postAjax but your endpoint route type is GET. Hence you may need to change your route type to POST

    Route::post(
        '/ahsdashdgasdhadgas',
        'Operation\BillingDepartmentController@get_collection_table'
    )->name('ahsdashdgasdhadgas');
    

    OR instead of postAjax use the ajax method of the builder

    $builder->ajax([
        'url' => route('users.index'),
        'type' => 'GET',
        'data' => 'function(d) { d.key = "value"; }',
    ])