skip to Main Content

Why is there always an error when doing more than seven days?

public function paginated(Request $request)
{
    $fromDate = $request->fromDate ? Carbon::parse($request->fromDate) : Carbon::now();
    $toDate = $request->toDate ? Carbon::parse($request->toDate) : Carbon::now();
    $query = Vehicle::with('make')
        ->with('class')
        ->with([
            'status' => function ($query) {
                $query->where('name', 'Active');
            }
        ])
        ->with('axleConfiguration')
        ->findRegNum($request)
        ->whereNotIn('registration_number', ['AA1460BA', 'AA8227OA', 'AA1916BA', 'AA8312OA', 'AA1918BA'])
        ->whereRaw("LEFT(registration_number, 6) != 'CHANGE' AND RIGHT(registration_number, 5) != '(OFF)'")
        ->whereBetween('last_inspection_date', [$fromDate, $toDate])
        ->orderBy('last_inspection_date', 'asc')
        ->paginate($request->perPage ?? 10);

    return self::successResponse($query, true);
}

Why, when it’s been less than seven days, there are no errors, but more than seven days, the error is like the following.

"message": "Illegal operator and value combination.",
"exception": "InvalidArgumentException",

enter image description here

2

Answers


  1. Chosen as BEST ANSWER

    When I lowered the todate time range to less than 7 days it worked

    "state": true,
    "message": "Data retrieved successfully",
    "data": [
    

    This is from Postman:

    {{BASE_URL}}/fleet/vehicle/get/paginated?perPage=50&page=1&fromDate=2023-12-01&toDate=2023-12-06
    

    But when the todate is more than 7 days it throws an error:

    "message": "Illegal operator and value combination.",
    "exception": "InvalidArgumentException",
    "file": "C:\yoshi\BACKEND\laravel\tyrecheck\api\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php",
    "line": 820,
    "trace": [
    

    This is the postman:

    {{BASE_URL}}/fleet/vehicle/get/paginated?perPage=50&page=1&fromDate=2023-12-01&toDate=2023-12-08
    

  2. The error you’re encountering, "Illegal operator and value combination," suggests that there might be an issue with the way you are using the whereBetween method in your query. It’s likely related to the format of the date values you are passing.

    When using whereBetween, it’s essential to ensure that the date values are in a valid format and that the database column you are comparing against is also of a date or datetime type.

    In your code, you are parsing the request dates using Carbon:

    $fromDate = $request->fromDate ? Carbon::parse($request->fromDate) : Carbon::now();
    $toDate = $request->toDate ? Carbon::parse($request->toDate) : Carbon::now();
    

    However, if the date formats are not consistent or if there’s an issue with parsing, it can result in an error. Additionally, make sure that the last_inspection_date column in your database is of a date or datetime type.

    To troubleshoot and fix the issue:

    1. Check Date Formats:

    Dump the values of $fromDate and $toDate using dd($fromDate, $toDate) just before the whereBetween clause. Ensure that the date formats are as expected.

    1. Validate Date Formats:

    If the date formats are not consistent, you may need to explicitly specify the format using the format method. For example:

    $fromDate = $request->fromDate ? Carbon::createFromFormat('Y-m-d', $request->fromDate) : Carbon::now();
    $toDate = $request->toDate ? Carbon::createFromFormat('Y-m-d', $request->toDate) : Carbon::now();
    
    1. Check Database Column Type:

    Make sure that the last_inspection_date column in your database is of a date or datetime type.

    1. Use Different Approach:

    Instead of whereBetween, you can try using two separate where clauses for greater than or equal to the start date and less than or equal to the end date:

    ->where('last_inspection_date', '>=', $fromDate)
    ->where('last_inspection_date', '<=', $toDate)
    

    This can sometimes be more reliable, especially if there are issues with the whereBetween method.

    By checking and adjusting these aspects, you should be able to identify and resolve the issue with the date filtering in your query.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search