TL;DR: There is no 2020-08-09 in the json response.
I just found a weird behavior in my Laravel 7.x project today. As mentioned on the title above, the created_at and updated_at of json response is different from the one generated from artisan command.
The problem appears when I change the default timezone in app/config.php from UTC to Asia/Jakarta. To make it clear, here is some result of my queries:
mysql> select * from field_reports
+----+------------+-----------+---------+-------------+------------------------+--------------------------------------------+---------------------+---------------------+
| id | company_id | branch_id | user_id | title | chronology | images | created_at | updated_at |
+----+------------+-----------+---------+-------------+------------------------+--------------------------------------------+---------------------+---------------------+
| 1 | 1 | 1 | 1 | example | example chronology | [{"url": "some_url", "path": "some_path"}] | 2020-08-08 16:02:53 | 2020-08-08 16:29:46 |
| 2 | 1 | 1 | 1 | new fr | new chronology | [{"url": "some_url", "path": "some_path"}] | 2020-08-08 18:31:32 | 2020-08-08 18:31:32 |
| 3 | 1 | 1 | 1 | new fr | new chronology | [{"url": "some_url", "path": "some_path"}] | 2020-08-08 18:32:48 | 2020-08-08 18:32:48 |
| 4 | 1 | 1 | 1 | example | example chronology | [{"url": "some_url", "path": "some_path"}] | 2020-08-09 01:33:31 | 2020-08-09 01:33:31 |
| 5 | 1 | 1 | 1 | NEW | NEW CHRONOLOGY | [{"url": "some_url", "path": "some_path"}] | 2020-08-09 01:44:41 | 2020-08-09 01:44:41 |
+----+------------+-----------+---------+-------------+------------------------+--------------------------------------------+---------------------+---------------------+
5 rows in set (0.02 sec)
php artisan tinker
>>> App\User::first()->fieldReports;
=> Illuminate\Database\Eloquent\Collection {#4033
all: [
App\FieldReport {#4035
id: 2,
company_id: 1,
branch_id: 1,
user_id: 1,
title: "new fr",
chronology: "new chronology",
images: "[{"url": "some_url", "path": "some_path"}]",
created_at: "2020-08-08 18:31:32",
updated_at: "2020-08-08 18:31:32",
},
App\FieldReport {#4038
id: 3,
company_id: 1,
branch_id: 1,
user_id: 1,
title: "new fr",
chronology: "new chronology",
images: "[{"url": "some_url", "path": "some_path"}]",
created_at: "2020-08-08 18:32:48",
updated_at: "2020-08-08 18:32:48",
},
App\FieldReport {#4039
id: 4,
company_id: 1,
branch_id: 1,
user_id: 1,
title: "example",
chronology: "example chronology",
images: "[{"url": "some_url", "path": "some_path"}]",
created_at: "2020-08-09 01:33:31",
updated_at: "2020-08-09 01:33:31",
},
App\FieldReport {#4040
id: 5,
company_id: 1,
branch_id: 1,
user_id: 1,
title: "NEW",
chronology: "NEW CHRONOLOGY",
images: "[{"url": "some_url", "path": "some_path"}]",
created_at: "2020-08-09 01:44:41",
updated_at: "2020-08-09 01:44:41",
},
],
}
>>>
and this one from get request in POSTMAN, here is where the problem exist, please have a look at the timestamp:
{
"success": true,
"data": [
{
"id": 2,
"company_id": 1,
"branch_id": 1,
"user_id": 1,
"title": "new fr",
"chronology": "new chronology",
"images": "[{\"url\": \"some_url\", \"path\": \"some_path\"}]",
"created_at": "2020-08-08T11:31:32.000000Z",
"updated_at": "2020-08-08T11:31:32.000000Z"
},
{
"id": 3,
"company_id": 1,
"branch_id": 1,
"user_id": 1,
"title": "new fr",
"chronology": "new chronology",
"images": "[{\"url\": \"some_url\", \"path\": \"some_path\"}]",
"created_at": "2020-08-08T11:32:48.000000Z",
"updated_at": "2020-08-08T11:32:48.000000Z"
},
{
"id": 4,
"company_id": 1,
"branch_id": 1,
"user_id": 1,
"title": "example",
"chronology": "example chronology",
"images": "[{\"url\": \"some_url\", \"path\": \"some_path\"}]",
"created_at": "2020-08-08T18:33:31.000000Z",
"updated_at": "2020-08-08T18:33:31.000000Z"
},
{
"id": 5,
"company_id": 1,
"branch_id": 1,
"user_id": 1,
"title": "NEW",
"chronology": "NEW CHRONOLOGY",
"images": "[{\"url\": \"some_url\", \"path\": \"some_path\"}]",
"created_at": "2020-08-08T18:44:41.000000Z",
"updated_at": "2020-08-08T18:44:41.000000Z"
}
]
}
Finally, here is the script in the controller:
FieldReportController.php
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$fieldReports = $request->user()->fieldReports;
return response()->json([
'success' => true,
'data' => $fieldReports,
]);
}
I've tried several things like:
- Run
php artisan config:clear - Run
php artisan cache:clear - Run
php artisan route:clear - Run
valet restart
But the problem is still there. Any clue?