Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

konrms's avatar

Date format in laravel controller

Hello guys.

I retrieve some data from my database student_data table. The date of birth inside the table is 19/04/95. (The format is dd/mm/yy). The data is retrieved with this command:

$d = DB::table('student_data')->value('date_of_birth');

But testing with dd($d) gives me this result:

"1995-04-19 00:00:00"

How can I alter dd($d) result to show date as follows?

19/04/1995

Thanks in advance.

0 likes
12 replies
CorCronje's avatar

I believe when retrieving a datetime value from your DB via Eloquent, the returned property would be a Carbon instance, and thus the following should work.

dd($d->format('dd/mm/Y'));

Carbon Docs

Best,

1 like
konrms's avatar

@CORCRONJE - Hi @corcronje

I tested it but this returns error:

Call to a member function format() on string

I also got the same error when I alter my code as

$d->format('dd/mm/Y');
dd($d);

In fact I want to pass this date data to a blade. At least, is it possible to alter date format in blade?

CorCronje's avatar

Hi there,

Please try as per the following:

$d = DB::table('student_data')->first()->date_of_birth //returns a string value via the query builder

$d = App\StudentData::first()->created_at //returns a Carbon instance via Eloquent

$d->format('d/m/Y'); // "12/05/2019"

Then in you blade template you can do somethong like,

{{ $student->date_of_birth->format('d/m/Y') }}

Regards,

1 like
Nakov's avatar

@konrms - Or adding date_of_birth in the $dates array of the model will return back a Carbon instance.

So presumably your model is called StudentData

You can add this in the model:

protected $dates = ['date_of_birth'];

Then you can do:

StudentData::findOrFail(1)->date_of_birth->format('d/m/Y');
CorCronje's avatar

Yes, or I completely forgot about about date casting:

You can add this to your StudentData model:

protected $casts = [
    'date_of_birth' => 'datetime:d/m/Y',
];

Casting works with serialization, thus this should work,

$objStudent = StudentData::first(); // get the first row
$arrStudent = $objStudent->toArray(); //serialize to an array
$d = $arrStudent['date_of_birth']; //15/05/2019

// can also string it together
$d = StudentData::first()->toArray()['date_of_birth']; //15/05/2019

But it is too long and messy.

I would rather just create an accessor on the StudentData model:

public function getBirthDateAttribute()
{
        return $this->date_of_birth->format('d/m/Y');
}

Then in Blade you can simply do,

{{ $student->birth_date }} //15/05/2019

Anyhow, hope this helps.

konrms's avatar

Hi @corcronje and @nakov

unfortunately I get errors. I tried this at my blade

{{ \Carbon\Carbon::parse($mydate->endda)->format('d/m/Y')}}

but also fails...

CorCronje's avatar

@KONRMS - It is a bit difficult to see without the code for retrieving the entity.

Lets work on a cleaner approach. are we still talking about the "student_data" table and is there a StudentData model?

konrms's avatar

@corcronje

Thanks for helping me!

My table ( 1x5) is this:

first_name last_name id date_of_birth city Dimitra Ioan 312312 19/04/95 New York

  1. Controller (only consider 'mydate => $d):

My query is this

$d = DB::table('student_data')->value('date_of_birth');

//code blalba...

return view('exagogi', ['epwnymo' => $a, 'onoma' => $b, 'arithmos' => $c, 'mydate => $d, 'xwra' => $e]);
  1. exagogi.blade
<!DOCTYPE html>


<head>

....

</head>
<body>

....

1.4 &nbsp;Date of Birth (day/month/year):&nbsp;{{$mydate}}  <!-- the result here is "1995-04-19 00:00:00" -->

....
</body>
<html>
CorCronje's avatar
Level 6

Pleasure man!

This line will return a string,

$d = DB::table('student_data')->value('date_of_birth');

Now lets overwrite $d with a Carbon instance from the string $d and reformat it like so,

$d = Carbon::createFromFormat('Y-m-d H:i:s', $d)->format('d/m/Y');

Then you can pass $d to the view/blade as before,

...['mydate' => $d]...

Then in blade you can do,

{{ $mydate }}
2 likes
konrms's avatar

@CORCRONJE - That's it! It worked!

I know it returns string. I may alter that later but it's on purpose for now.

Thanks a lot!

Please or to participate in this conversation.