Home > Net >  How to filter data after particular date in laravel 8
How to filter data after particular date in laravel 8

Time:08-13

I want to filter the data between the dates. I have used WhereBetween but it is not working.

table data

Here is my code

  $from = Carbon::parse($req->start_date_of_registration);
            $to = Carbon::parse($req->end_date_of_registration);
            $data = tbl_company::whereBetween(
                "date_of_registration",
                [
                    $from,
                    $to
                ]
            )
                ->paginate(100);

CodePudding user response:

whereDate works only if database table column data type is timestamp or datetime or date .Since you are storing date as varchar (example value in date_of_registration column is 12-08-2022) ,you have to use raw query like below

 $data = tbl_company::whereBetween(
        DB::Raw("STR_TO_DATE(date_of_registration,'%d-%m-%Y')"), [ $from,$to])
        ->paginate(100);

CodePudding user response:

try this code

$start_date = Carbon::parse($request->start_date)->toDateTimeString();

$end_date = Carbon::parse($request->end_date)->toDateTimeString();

User::whereBetween('created_at', [
         $start_date,
         $end_date
      ])->get();

CodePudding user response:

send date as string format (Y-m-d) 1997-04-30

or use strtotime to formate date

$data = tbl_company::whereBetween(
            "date_of_registration",
            [
                date('Y-m-d',strtotime($from)),
                date('Y-m-d',strtotime($to))
            ]
        )
            ->paginate(100);
  • Related