使用幼虫搜索实时数据,但条件无法正常运行。实际上,我想要将is_admin列值分配为NULL或2并且id大于1的结果。但是当我搜索时,他们没有遵循这些条件。
public function searchUserregistrationlist(Request $request)
{
if($request->ajax())
{
$output = '';
$query = $request->get('query');
if($query != '')
{
$userreg = DB::table('users')
->where('is_admin', '=', NULL)
->orWhere('is_admin', '=' , 2)
->where('id', '>', 1)
->where('name', 'like', '%'.$query.'%')
->orWhere('email', 'like', '%'.$query.'%')
->orWhere('mobile_no', 'like', '%'.$query.'%')
->orWhere('country', 'like', '%'.$query.'%')
->get();
}
$total_row = $userreg->count();
if($total_row > 0)
{
foreach($userreg as $userregs)
{
$output .= '
<tr>
<td>'.$userregs->name.'</td>
<td>'.$userregs->email.'</td>
<td>'.$userregs->mobile_no.'</td>
<td>'.$userregs->country.'</td>
<td>'.$userregs->created_at.'</td>';
if($userregs->is_admin == 2){
$output .='<td style="color:red">Block</td>';
}elseif ($userregs->is_admin == "NULL"){
$output .='<td style="color:green">Active</td>';
}else{
$output .='<td style="color:green">Active</td>';
}
$output .='<td><a href="userupdatelist/'.$userregs->id .'" class="btn btn-primary"><span class="glyphicon glyphicon-pencil"></span></i></a>
<a href="userregistrationlist/'. $userregs->id .'" class="btn btn-danger"><span class="glyphicon glyphicon-trash"></span></a>
</td>
</tr>
';
}
}
else
{
$output = '
<tr>
<td align="center" colspan="5">No Any Data List Found</td>
</tr>
';
}
$userreg = array(
'table_data' => $output,
'total_data' => $total_row
);
echo json_encode($userreg);
}
}
这是我给路由web.php的根文件
Route::get('/searchUserregistrationlist', 'AdminController@searchUserregistrationlist');
/ *查看页面和脚本* /
<input type="text" name="searchUserregistrationlist" id="searchUserregistrationlist" placeholder="Search..." class="form-control">
<script>
$(document).ready(function(){
fetch_usereg_data();
function fetch_usereg_data(query = '')
{
$.ajax({
url:"{{ URL::to('/searchUserregistrationlist') }}",
method:'GET',
data:{query:query},
dataType:'json',
success:function(data)
{
$('tbody').html(data.table_data);
$('#total_records').text(data.total_data);
}
})
}
$(document).on('keyup', '#searchUserregistrationlist', function(){
var query = $(this).val();
fetch_usereg_data(query);
});
});
</script>
答案 0 :(得分:1)
查看您的口才来生成查询,这是解决方案
$userreg = DB::table('users')
->where('id', '>', 1)
->where(function ($q) {
$q->whereNull('is_admin')
->orWhere('is_admin', '=', 2);
})
->where(function ($q) use($query) { // You can pass the necessary variables from the parent scope into the closure with the use keyword.
$q->where('name', 'like', '%' . $query . '%')
->orWhere('email', 'like', '%' . $query . '%')
->orWhere('mobile_no', 'like', '%' . $query . '%')
->orWhere('country', 'like', '%' . $query . '%');
})->get();
这是使用advanced where clause的链接。