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

latz's avatar
Level 3

Format Date - Json output

I have problems with date-format in json-output.

In the model i use an accessor

public function getdatumAttribute($value) {
return Carbon::parse($value)->format('d.m.Y');
  }

With eloquent this works fine. I've got the correct format of the datefield.

If i use the select:

$player = DB::table('players')->select(DB::raw("*, 
....
return $player;

the accessor doesn't work und the output of any date is wrong (for me). Why does the accessor not work with the db-select and how can i format the date in json output?

best regards

0 likes
2 replies
konstantinrachev's avatar

The accessor is working when you use it through your model. The code below is based on the query builder. Try

$player = Player::where('', '')->firstOrFail();
latz's avatar
Level 3

thanks for your reply.

I need the query bilder for a complex select.

Is it possible to format the date in the sql select? I use postgresql. Any Ideas?

Please or to participate in this conversation.