Home > Blockchain >  How to get data filter date according to field table from database
How to get data filter date according to field table from database

Time:03-11

enter image description here enter image description here enter image description here

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

        }

    }
} 

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 ?

CodePudding user response:

normally whereBetween using for single column date check. but in this case youu 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.

CodePudding user response:

whereBetween function is used to query one field with 2 or more values, what you really want is just where function twice, try this:

...
      ->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();
...
  • Related