我已经尝试了很多方法来与laravel Yajara Datatbales配合使用日期范围过滤器 这是我的模型范围的代码:
public function scopeBetween($query,$from,$to)
{
return $query->whereBetween('orders.created_at',[$from,$to]);
}
我的控制器:
public function getOrders(Request $request)
{
## Read value
$from = "2020/09/01";
$to = "2020/09/02";
// $from = $request->get('from');
// $to = $request->get('to');
$method = $request->get('method');
$draw = $request->get('draw');
$start = $request->get("start");
$rowperpage = $request->get("length"); // Rows display per page
$columnIndex_arr = $request->get('order');
$columnName_arr = $request->get('columns');
$order_arr = $request->get('order');
$search_arr = $request->get('search');
$columnIndex = $columnIndex_arr[0]['column']; // Column index
$columnName = $columnName_arr[$columnIndex]['data']; // Column name
$columnSortOrder = $order_arr[0]['dir']; // asc or desc
$searchValue = $search_arr['value']; // Search value
// Total records
$totalRecords = Order::select('count(*) as allcount')->between('orders.created_at', [$from, $to])->count();
$totalRecordswithFilter = Order::select('count(*) as allcount')->between('orders.created_at', [$from, $to])->where('client_id', 'like', '%' . $searchValue . '%')->count();
// Fetch records
$records = Order::orderBy($columnName, $columnSortOrder)
->between('created_at', [$from, $to])
->join('clients', 'orders.client_id', '=', 'clients.id')
->join('products', 'orders.product_id', '=', 'products.id')
->join('adresses', 'orders.adress_id', '=', 'adresses.id')
->join('cities', 'adresses.city_id', '=', 'cities.id')
->join('governorates', 'adresses.gov_id', '=', 'governorates.id')
->join('types', 'orders.type_id', '=', 'types.id')
->join('users', 'orders.transfered_to', '=', 'users.id')
->where('clients.name', 'like', '%' . $searchValue . '%')
->orWhere('cities.name', 'like', '%' . $searchValue . '%')
->orWhere('governorates.gov_name', 'like', '%' . $searchValue . '%')
->orWhere('types.name', 'like', '%' . $searchValue . '%')
->orWhere('users.name', 'like', '%' . $searchValue . '%')
->orWhere('products.name', 'like', '%' . $searchValue . '%')
->select('orders.*')
->skip($start)
->take($rowperpage)
->get();
$data_arr = array();
foreach ($records as $record) {
$orders = $record;
$clients = $record->client;
$users = $record->company_transfered_to;
$products = $record->product_type;
$types = $record->order_type;
$adresses = $record->adress;
$governorates = $record->adress->gov;
$cities = $record->adress->city;
$data_arr[] = array(
"orders" => $orders,
"clients" => $clients,
"products" => $products,
"types" => $types,
"cities" => $cities,
"governorates" => $governorates,
"users" => $users,
);
}
$response = array(
"draw" => intval($draw),
"iTotalRecords" => $totalRecords,
"iTotalDisplayRecords" => $totalRecordswithFilter,
"aaData" => $data_arr
);
echo json_encode($response);
exit;
}
我的脚本代码:
$(document).ready(function() {
// DataTable
oTable = $('#orderTable').DataTable({
processing: true,
serverSide: true,
ajax: {
url: "{{route('getOrders')}}",
data: {
from: $('#from').val(),
to: $('#to').val(),
}
},
columns: [{
data: 'orders.id'
},
{
data: 'clients.name',
},
{
data: 'clients.mobile'
},
{
data: 'governorates.gov_name'
},
{
data: 'cities.name'
},
{
data: 'types.name'
},
{
data: 'products.name'
},
{
data: 'orders.created_at',
"render": function(value) {
if (value === null) return "";
return moment(value).format('YYYY/MM/DD');
}
},
{
data: 'users.name'
},
],
select: true,
});
$('#to').on('change', function() {
oTable.draw();
});
});
我尝试操纵日期格式,但仍然无法正常工作 它总是为我提供模型中的所有数据,这意味着范围不起作用 即使我在控制器内进行查询(不使用范围),也是如此