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

noblemfd's avatar

How to carbon query output as dd-mm-yy

I have this Laravel Query:

$nationalholidays               = HrHolidayDate::select('holiday_date')->where('company_id', $userCompany)->whereYear('created_at', '=', date('Y'))->get();

How do I carbon the output, that is, holiday_date as dd-mm-yy

0 likes
5 replies
MichalOravec's avatar
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class HrHolidayDate extends Model
{
    /**
     * The attributes that should be mutated to dates.
     *
     * @var array
     */
    protected $dates = [
        'holiday_date',
    ];
}
@foreach (nationalholidays as $national)
    {{ $natial->holiday_date->format('d-m-y') }}
@endforeach

You can use now()->year

$nationalHolidays = HrHolidayDate::select('holiday_date')
    ->where('company_id', $company->id)
    ->whereYear('created_at', now()->year)
    ->get();
tykus's avatar

You can create a mutator on the HrHolidayDate model which formats the date as you need for display

public function getHolidayDateForDisplayAttribute()
{
	return $this->holiday_date->format('d-m-y'); // assumes it is already cast as Carbon instance
}

This provides you with a computed property on the model instances holiday_date_for_display without affecting other places where the holiday_date property would be expected to be a Carbon instance.

noblemfd's avatar

@michaloravec - I have already done:

   protected $dates = [
       'holiday_date',
   ];

But I am passing it from the controller as JSON:

public function findLeaveCount(Request $request)
{
   $nationalholidays               = HrHolidayDate::select('holiday_date')->where('company_id', $userCompany)->whereYear('created_at', '=', date('Y'))->get();
    return response()->json([
        'nationalholidays' => $nationalholidays,
    ]);           
}

view

    <script type="text/javascript">
        var holidayDays = [];
    $(document).ready(function() {
        $(document).on('change', '#leave_type', function() {
            var air_id =  $(this).val();

            var a = $(this).parent();

            var op = "";
            
            $.ajax({
                type: 'get',
                url: '{{ route('get.leavecounts.all') }}',
                data: { 'id': air_id },
                dataType: 'json',      //return data will be json
                // console.log("Its Change !");
                success: function(data) {
                     holidayDays = data.nationalholidays;
                },
                error:function(){

                }
            });
        });
    });
    </script>

But it comes out as in console:

0:
holiday_date: "2020-08-25 00:00:00"
__proto__: Object
1:
holiday_date: "2020-08-26 00:00:00"
__proto__: Object
2: {holiday_date: "2020-09-25 00:00:00"}
3: {holiday_date: "2020-11-30 00:00:00"}

__proto__: Array(0)

Please or to participate in this conversation.