laravel 数据表中的自定义查询

custom query in laravel datatable

我正在尝试对 table 喜欢的人进行查询

tbl_bottle

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

所以当我加载前端时它会显示 这个

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

但是当我在搜索中输入 bot1 时,它应该给我:

name | type | location
bot1    A       USA
bot1    B       

但我得到的是

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

这就是我控制器中的内容

     $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);

所以数据table 正确显示了这个但是当我尝试搜索时它不能正常工作 我的意思是当我搜索

所以在我的前端我只有

<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>

搜索输入被数据插入tables 我使用的文档是 https://github.com/yajra/laravel-datatables

如果您想通过瓶名进行搜索,那么首先您必须将搜索key word(瓶名)与请求一起发送,这样您就可以得到它在方法中。

在您的 view 中发送搜索 key word:

<form>
  <input type="text" name="key_word" />
  <button type="submit">Submit</button>
</form> 

在您的 controller 中,获取 key word

获取关键字并将其分配给变量,然后在查询瓶子时将变量添加到 where clause 中,如下所示:

public function getBottles(Request $request){
  $keyWord = $request->key_word; // get the key word and assign it to a variable 
  $bottle= tbl_bottle::select(
                'name',
                'type',
                'location'            
            )->where('name','=', $keyWord)->

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);
}

Before you straight away take a user input into process, you have to validate it, but those things are besides this question boundary. My answer is only for what you have asked for.

所以这对我有用

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

return DataTables::of($bottle)->make(true);

使用此代码(用于原始查询)

$searchValue = $request->input('search')['value']; // Search value from datatable
//-- END DEFAULT DATATABLE QUERY PARAMETER

//-- START DYNAMIC QUERY BINDING
$conditions = '1 = 1';
if (!empty($searchValue)) {
    $conditions .= " AND name LIKE '%" . trim($searchValue) . "%'";
}
//-- END DYNAMIC QUERY BINDING

//-- WE MUST HAVE COUNT ALL RECORDS WITHOUT ANY FILTERS
$countAll = \App\tbl_bottle::count();

//-- CREATE DEFAULT LARAVEL PAGING
$paginate = \App\tbl_bottle::select('*')
    ->whereRaw($conditions)
    ->paginate($limit, ["*"], 'page', $page);

或者您可以查看我的博客post Laravel datatables了解更多详情。它也提供排序和搜索。