I tried to display data in the database, the date format in the database is like 2020-10-11T17: 22: 29.000000Z ", how can i change it just to 2020-10-11, or 11 December 2020 ?
This is my controller
public function get_all_artikel(){
$data = ArtikelKonten::select(
'artikel_kategori.nama as kategori','artikel_konten.*')
->join('artikel_kategori','artikel_kategori.id','artikel_konten.id_kategori')
->get();
if ($data){
return response()->json([
'status' => true,
'artikel' => $data,
],200);}
else{
return response()->json([
'status' => false,
'message' => 'No Artikel were found'
],404);}
}
This is my model
class ArtikelKonten extends Model
{
protected $table = 'artikel_konten';
protected $fillable = ['id_kategori', 'gambar', 'tag_program', 'nm_program', 'judul', 'preview', 'konten'];
const CREATED_AT = 'created';
const UPDATED_AT = 'modified';
}
And this is the result
{
"status": true,
"artikel": [
{
"kategori": "Program",
"id": 4,
"id_kategori": "2",
"tag_program": "2",
"nm_program": "Zakat Mall",
"gambar": "http://127.0.0.1:8000/storage/photos/1/article1.png",
"judul": "Mengenalkan Zakat Kepada Anak",
"preview": null,
"konten": null,
"created": "2020-12-10T07:24:50.000000Z",
"modified": "2020-12-10T08:06:07.000000Z"
},
{
"kategori": "Berita",
"id": 10,
"id_kategori": "1",
"tag_program": "4",
"nm_program": "Jumat Barokah",
"gambar": "http://127.0.0.1:8000/storage/photos/1/article2.png",
"judul": "Suplemen Iman Ditengah Pandemi",
"preview": null,
"konten": null,
"created": "2020-12-11T20:44:25.000000Z",
"modified": "2020-12-11T20:44:25.000000Z"
},
{
"kategori": "Program",
"id": 11,
"id_kategori": "2",
"tag_program": "2",
"nm_program": "Zakat Mall",
"gambar": "http://127.0.0.1:8000/storage/photos/1/article3.png",
"judul": "Menumbuhkan Semangat Berzakat Umat",
"preview": null,
"konten": null,
"created": "2020-12-11T20:46:23.000000Z",
"modified": "2020-12-11T20:46:23.000000Z"
}
]
}
I just wana change the "created": "2020-12-11T20:46:23.000000Z", to "create":"2020-12-11", thanks for your answer guys :)
Laravel 7 uses a new date serialization format when using the toArray or toJson method on Eloquent models.
If you would like to keep using the previous behavior you can override the serializeDate()
method on your model :
use DateTimeInterface;
protected function serializeDate(DateTimeInterface $date)
{
return $date->format('Y-m-d');
}
See the official upgrade doc here [7.x]