1

I'm trying to get the value of an input type text to use it for the where clause in my Laravel controller query.

here is a part of the ajax code:

$(function() {
    $('#data-table').DataTable({
        "processing": true,
        "serverSide": true,
        "ajax":{
             "url": "{{ route('item_data_table') }}",
             "dataType": "json",
             "type": "POST",
             "data":{ _token: "{{ csrf_token() }}"}
        },
        ...
        ...
        ...
    });
});

as for my trial, I tried changing:

"data":{ _token: "{{ csrf_token() }}"}

into:

"data":{value: $("input[name=categoryname]").val()}

Then in my controller, I have this part of query code, Tried it like this:

public function dataTable(Request $request)
    {
        $columns = [
            1 => 'id',
            2 => 'item_name',
            3 => 'item_category_name',
            4 => 'item_detail_category_name',
        ];

        $categoryname = Input::get('value');

        $totalData = MItem::count();
        $totalFiltered = $totalData;
        $limit = request()->length;
        $start = request()->start;
        $order = $columns[request()->order[0]['column']];
        $dir = request()->order[0]['dir'];
        $items = MItem::select('m_item.id',
                        'm_item.item_name',
                        'm_item_category.item_category_name',
                        'm_item_detail_category.item_detail_category_name'
                    )
                    ->join('m_item_category', 'm_item.item_category_id', 'm_item_category.id')
                    ->join('m_item_detail_category', 'm_item.item_category_detail_id', 'm_item_detail_category.id');
                    if($categoryname) {
                        $items->where('m_item_category.item_category_name', 'like', '%'.$userID.'%');
                    }
                    $items->offset($start)
                    ->limit($limit)
                    ->orderBy($order, $dir)
                    ->get();
        $data = [];

...
...
...
}

I used $cateogryname for my where clause but cannot succesfully do it.

How can be the code done?

3 Answers 3

3

Try to pass data like this

 data: function (d) {
            d.name = $('input[name=name]').val();
            d.email = $('input[name=email]').val();
        }

u can find example here https://datatables.yajrabox.com/collection/custom-filter

Sign up to request clarification or add additional context in comments.

Comments

3

You don't need to manually pass the token. Just set up it once and use it for all Ajax calls:

<meta name="csrf-token" content="{{ csrf_token() }}">

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

This will also allow you to not mix PHP with JS.

Comments

0

Thanks for giving me ideas guys, somehow I did it like this, and got my search function working:

for the ajax code:

"ajax":{
         "url": "{{ route('item_data_table') }}",
         "dataType": "json",
         "type": "POST",
         "data":{
                _token: "{{ csrf_token() }}",
                'item_detail_category_name': $('#item_detail_category_name').val(),
                'item_category_name': $('#item_category_name').val(),
         }
}

and for the controller condition,

 $limit = request()->length;
        $start = request()->start;
        $order = $columns[request()->order[0]['column']];
        $dir = request()->order[0]['dir'];
        $items = MItem::select('m_item.id',
                        'm_item.item_name',
                        'm_item_category.item_category_name',
                        'm_item_detail_category.item_detail_category_name'
                    )
                    ->join('m_item_category', 'm_item.item_category_id', 'm_item_category.id')
                    ->join('m_item_detail_category', 'm_item.item_category_detail_id', 'm_item_detail_category.id');

        if(request()->item_category_name) {
            $items->where('m_item_category.item_category_name', 'like', '%'.request()->item_category_name.'%');
        }

        if(request()->item_detail_category_name) {
            $items->where('m_item_detail_category.item_detail_category_name', 'like', '%'.request()->item_detail_category_name.'%');
        }

        $totalData = $items->get()->count();
        $totalFiltered = $totalData;

        $data = [];

        $items->offset($start)->limit($limit)->orderBy($order, $dir);

        $items = $items->get();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.