'JSON Render Issue in Date Object Laravel and PHP 7.4
I am facing a stranger issue in php 7.4 inside Laravel.
When I am trying to log the results using dd() function for any collection its gives me that my date fields is saved and working fine like the following:
But when I am trying to print the collection as JSON response to use it as API the dates fields is not working fine like the following:
the confirm_time field should be look like the done_date field but its just showing a text date instead of data object and I don't know why?
MY PHP Code:
<?php
namespace App\Http\Controllers\Syncv1;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class SyncBookingDataController extends Controller
{
public function __construct()
{
}
public function index()
{
$data['status'] = 1;
$get_onlinebooking_items = \App\Models\Booking_items::where('off_mode','0')
->where('type', 'confirmed')
->skip(0)
->take(250)
->get();
$data['bookings_items'] = $get_onlinebooking_items;
return response()->json($data);
}
}
?>
Thanks
Solution 1:[1]
dd() function just show the information as an html and formated. But when you insert the document in your datebase, the information is passing through Laravel treatment, the solution is in your model Booking_items, look if you have this line:
protected $dates = ['created_at', 'updated_at'];
This property tells the aplication to format the dates, so if the date are in, Laravel will automaticaly format to you, if is not, you need to do by yourself.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Erick Henrique |