Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle DateTime values better #3156

Closed
miken32 opened this issue Jul 5, 2024 · 2 comments · Fixed by #3163
Closed

Handle DateTime values better #3156

miken32 opened this issue Jul 5, 2024 · 2 comments · Fixed by #3163
Labels

Comments

@miken32
Copy link
Contributor

miken32 commented Jul 5, 2024

Summary of problem or feature request

In the Helper::transformRow() method there is special code for nicely formatting a DateTime object into a string value. However, "processing" is done before "transforming" so the Helper::convertToArray() method has already converted all DateTimes into arrays.

Code snippet of problem

$data = [["a_date" => new DateTime()], ["a_date" => new DateTime('tomorrow')]];
$table = Yajra\DataTables\Facades\DataTables::collection($data)->make();
dump($table);

This results in something like this:

= Illuminate\Http\JsonResponse {#6936
    +headers: Symfony\Component\HttpFoundation\ResponseHeaderBag {#6935},
    +original: [
      "draw" => 0,
      "recordsTotal" => 2,
      "recordsFiltered" => 2,
      "data" => [
        [
          "a_date" => [
            "date" => "2024-07-05 17:06:52.608468",
            "timezone_type" => 3,
            "timezone" => "America/Toronto",
          ],
        ],
        [
          "a_date" => [
            "date" => "2024-07-06 00:00:00.000000",
            "timezone_type" => 3,
            "timezone" => "America/Toronto",
          ],
        ],
      ],
      "input" => [],
    ],
    +exception: null,
  }

Where I think the intent (based on the code in Helper::transformRow()) is something like this:

= Illuminate\Http\JsonResponse {#6936
    +headers: Symfony\Component\HttpFoundation\ResponseHeaderBag {#6935},
    +original: [
      "draw" => 0,
      "recordsTotal" => 2,
      "recordsFiltered" => 2,
      "data" => [
        [
          "a_date" => "2024-07-05 17:06:52",
        ],
        [
          "a_date" => "2024-07-06 00:00:00",
        ],
      ],
      "input" => [],
    ],
    +exception: null,
  }

System details

  • PHP 8.3.8
  • Laravel 11.14.0
  • Laravel-Datatables 11.1.2
@yajra
Copy link
Owner

yajra commented Jul 6, 2024

Yes correct, this was the intention and behavior in the previous versions. Thank you for digging into the details.

If you can, please do not hesitate to submit a PR. Thanks!

@miken32
Copy link
Contributor Author

miken32 commented Aug 9, 2024

PR submitted. I wanted to check and see when this behaviour changed and it looks like the array return is present in versions 10 and 9, but I couldn't test any earlier due to PHP version incompatibilities. So it has been like that for a while; I'll leave it to you to decide if there's a possibility of this change being disruptive.

@github-actions github-actions bot removed the stale label Aug 10, 2024
Repository owner deleted a comment from github-actions bot Aug 11, 2024
@yajra yajra added the pinned label Aug 11, 2024
@yajra yajra closed this as completed in 6c4f1e4 Aug 17, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Sep 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants