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

snehakulal7's avatar

I need to display dates like [{"date": "2019-01-25"}, {"date": "2019-01-12"}]

Hello everyone, I am back with new problem again.I have a table called online.In that table i have added two entries which contains two date that is jan 25 2019 and jan 12 2019. So in the controller i have written query like this to fetch the dates.

public function pre(){ $dates = DB::table('online') ->select('online.date') ->distinct() ->get(); return view('admin.pre',compact('dates'));

i am displaying it in pre.blade.php like this

@foreach($dates as $date)

{{$date->date}} @endforeach

So i will get the output be like 25-1-2019 12-1-2019

I need to display those dates like "2019-01-25", "2019-01-12".Please help me to solve this problem

0 likes
5 replies
snehakulal7's avatar

@VILFAGO - Its a date format not timestamp.I want to display fetched dates with commas in between atleast like "25-01-2019,12-1-2019".Thank you for your answer.

Vilfago's avatar

$date->date->toDateString();

If I remember well, it should be a Carbon Instance, so should work.

Else, try \Carbon::make($date->date)->toDateString();

If your issue is comma, just add it in your loop

@foreach($dates as $date)
[{"date": "{{$date->date->toDateString();}}"}]
 @if (!$loop->last)
        ,
  @endif
@endforeach

//or
@foreach($dates as $date)
{{$date->date->toDateString();}}
 @if (!$loop->last)
        ,
  @endif
@endforeach

If you want a json, use a native function : https://laravel.com/docs/5.7/eloquent-serialization#serializing-to-json

Vilfago's avatar

If it's working, close the thread choosing a best answer

Please or to participate in this conversation.