Mukesh Patel
Mukesh Patel

Reputation: 75

laravel datatable using ajax post method

How to call the jquery datatable using Ajax post method same as this link but in post method

public function getAdvanceFilterData(Request $request)
{
    $users = User::select([
        DB::raw("CONCAT(users.id,'-',users.id) as id"),
        'users.name',
        'users.email',
        DB::raw('count(posts.user_id) AS count'),
        'users.created_at',
        'users.updated_at'
    ])->leftJoin('posts', 'posts.user_id', '=', 'users.id')
    ->groupBy('users.id');

    $datatables =  app('datatables')->of($users)
        ->filterColumn('users.id', 'whereRaw', "CONCAT(users.id,'-',users.id) like ? ", ["$1"]);

    // having count search
    if ($post = $datatables->request->get('post')) {
        $datatables->having('count', $datatables->request->get('operator'), $post);
    }

    // additional users.name search
    if ($name = $datatables->request->get('name')) {
        $datatables->where('users.name', 'like', "$name%");
    }

    return $datatables->make(true);
}

Upvotes: 1

Views: 6842

Answers (1)

Bardh Lohaj
Bardh Lohaj

Reputation: 1410

When you initialize the DataTable on javascript, set the ajax method to post like this:

"type": "POST"

(adding it on the example that you linked):

    ajax: {
        url: 'http://datatables.yajrabox.com/eloquent/advance-filter-data',
        type: "POST",
        data: function (d) {
            d.name = $('input[name=name]').val();
            d.operator = $('select[name=operator]').val();
            d.post = $('input[name=post]').val();
        }
    },

and then on laravel you should be able to access the inputs in form of

$datatables->request->post('post')

Upvotes: 3

Related Questions