laravellaravel-datatables

custom query in laravel datatable


i am trying to do a query on a table likes so

tbl_bottle

name | type | location
bot1    A       USA
bot2    B       
bot3    C       USA
bot4    A        UK
bot5    A        UK

so when i load front end it will show this

name | type | location
bot1    A       USA
bot1    B       
bot3    C       USA
bot4    A        UK
bot5    A        UK

but when i type bot1 in search it should give me :

name | type | location
bot1    A       USA
bot1    B       

but what i get instead is

name | type | location
bot1    A       USA
bot1    B       
bot3    C       USA

this is what i have in my controller

     $bottle= tbl_bottle::select(
                'name',
                'type',
                'location'            
            )->where('location','=','USA')->OrWhere('location','=',' ');

return DataTables::of($bottle)
            ->addColumn('action', function ($bottle) {
                return '<a href="#" class="btn btn-xs btn-primary got=to" id="' . $members->name. '">View Details</a>';
            })->make(true);

so the datatable displays this correctly but when i try to search it does not work correctly what i mean is when i search

so in my front end all i have is

<table id="tbl_bottles" class="table">
<th>Name</th>
<th>Type</th>
<th>Location</th>
<th>Action</th>
</table>
    <script type="text/javascript">
$(document).ready(function () {
    $('#tbl_bottles').DataTable({
        "processing": true,
        "serverSide": true,
        "ajax": "{{ route('ajax.getBottles') }}",
        "columns": [
            { "data": 'name'},
            {"data": "type"},
            {"data": "location"},
            {"data":"action",orderable:false,searchable:false}
        ],
    });
});
</script>

the search input gets plugged in by the datatables the documentation iam using is https://github.com/yajra/laravel-datatables


Solution

  • so this worked for me

    $bottle= tbl_bottle::select(
      'name',
      'type',
      'location'            
    )->where(function($query) {
      $query->where('location','=','USA')->OrWhere('location','=',' ')
    });
    
    return DataTables::of($bottle)->make(true);