[英]How to get data filter date according to field table from database
我在 LaporanController.php 中的代碼
class LaporanController extends Controller
{
public function index(Request $request)
{
if (isset($request->start_date) && isset($request->end_date)) {
$start_date = Carbon::parse($request->start_date)->format('Y-m-d');
$end_date = Carbon::parse($request->end_date)->format('Y-m-d');
$attendance_absent = DB::table('attendance_absent as absent')
->whereBetween('absent.do_date_start', 'absent.do_date_end', [$start_date, $end_date])
->get();
dd($attendance_absent);
}
}
}
如何根據數據庫字段do_date_start和do_date_end中的attendance_absent表從start_date和end_date獲取請求數據? 我嘗試使用 whereBetween 但出現錯誤:Illuminate\Database\Query\Builder::whereBetween(): Argument #2 ($values) must be of type array, string given. 如何解決我的問題?
whereBetween
function用於查詢一個字段有2個或多個值,你真正想要的只是where
function兩次,試試這個:
...
->where([
['absent.do_date_start', '>=', $start_date],
['absent.do_date_end', '<=', $end_date],
])
->orWhere(function ($query) use ($start_date, $end_date) {
$query->where([
['absent.do_date_start', '>', $start_date],
['absent.do_date_start', '>', $end_date],
['absent.do_date_end', '<', $start_date],
['absent.do_date_end', '<', $end_date],
])
})
->get();
...
通常whereBetween
用於單列日期檢查。 但在這種情況下,您需要從不同的列中獲取信息。 所以試着像這樣檢查那些日期。 我認為我們會為您提供全面幫助。 像這樣嘗試
->whereDate('do_date_start', '>=', $from_date)
->whereDate('do_date_start', '<=', $to_date)
->whereDate('do_date_end', '>=', $from_date)
->whereDate('do_date_end', '<=', $to_date)
如果您使用whereBetween
,您將不會獲得今天相同的日期。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.