Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

babo's avatar
Level 1

How to get data filter date according to field table from database

Hi everyone. how to get request data from start_date and end_date according to attendance_absent table from database fields do_date_start and do_date_end? i try to use whereBetween but i get error : Illuminate\Database\Query\Builder::whereBetween(): Argument #2 ($values) must be of type array, string given. how to solve my problem ?

my code in 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);

        }

    }
} 
0 likes
15 replies
SilenceBringer's avatar

@babo not sure I understand your question correctly, but I think you're looking for something like

              $attendance_absent = DB::table('attendance_absent as absent')
                    ->where('absent.do_date_start',  '>=', $start_date)
                    ->where('absent.do_date_end',  '<=', $end_date)
                    ->get();
babo's avatar
Level 1

I've tried that method, when I select from the 14th to the 18th the correct data appears but for example, if I select the 15th to 17th the data becomes null

SilenceBringer's avatar

@babo because I didn't understand you question correctly, I think. Try

              $attendance_absent = DB::table('attendance_absent as absent')
                    ->where('absent.do_date_start',  '<=', $start_date)
                    ->where('absent.do_date_end',  '>=', $end_date)
                    ->get();
babo's avatar
Level 1

@SilenceBringer thank you, This method is correct, the data I get, right, but I have a new problem, when I select the 14th and 19th the data is empty, there should be data because on the 14th to 18th the data is in the database.

Nihir's avatar

You can try JS or Jquery to filter the data according to your filter type

SilenceBringer's avatar

@Nihir bad suggestion. Huge performance penalty to grab all the data and then filter it on client side

Michael88's avatar

Hi, why do you have

$attendance_absent = DB::table('attendance_absent as absent')
                    ->whereBetween('absent.do_date_start',   'absent.do_date_end', [$start_date, $end_date])
                    ->get();

shouldn't it be

$attendance_absent = DB::table('attendance_absent as absent')
                    ->whereBetween('absent.do_date', [$start_date, $end_date])
                    ->get();
babo's avatar
Level 1

@Michael88 if using this data I can only retrieve the do_date_start data, while the do_date_end is null

Michael88's avatar

@babo so what if

$attendance_absent = DB::table('attendance_absent as absent')
                    ->whereBetween('absent.do_date_start', [$start_date, $end_date])
					->andWhereNull('absent.do_date_end') //If I understand you right
                    ->get();
babo's avatar
Level 1

@Michael88 i've get error : Call to undefined method Illuminate\Database\Query\Builder::andWhereNull()

MohamedTammam's avatar
$attendance_absent = DB::table('attendance_absent as absent')
	->whereBetween('absent.do_date_start',[$start_date, $end_date])
	->whereBetween( 'absent.do_date_end',[$start_date, $end_date]) 
    ->get();
babo's avatar
Level 1

@MohamedTammam when i get data start_date is greater than do_date_start and end_date is less than do_date_end the data is null, it should be

2048tm's avatar

normally whereBetween using for single column date check. but in this case you need get from the different columns. so try to check those date like this. i think it will we help full for you. do try it like this

->whereDate('do_date_start', '>=', $from_date)
->whereDate('do_date_start', '<=', $to_date)
->whereDate('do_date_end', '>=', $from_date)
->whereDate('do_date_end', '<=', $to_date)

other thin if you used whereBetween you will not get equal date of today. You may be find instruction at https://cupcake-2048.com

Please or to participate in this conversation.