Home > database >  Laravel how to display day and month only without year from date type?
Laravel how to display day and month only without year from date type?

Time:09-18

I would wan to display month and data for payment_date only in a foreach loop in view.blade.php.

  @foreach ($alert as $key=>$data)
          <tr>
             <td>{{   $key }}</td>
             <td>{{ $data->tenant_name}}</td>
             <td>{{ $data->property_name}}</td>
             <td>RM {{ $data->amount}}</td>
             <td>{{ $data->payment_date }}</td>
             <td><span >{{ $data->status }}</span></td>                                       
             <td><a href="{{ route('view.email', $data->tenant->id) }}"   title="Send Email"> <i ></i> </a>
             </td>
          </tr>
   @endforeach

enter image description here

Hereby I include the datatype: enter image description here

CodePudding user response:

Short way (not recommended)

@foreach ($alert as $key=>$data)
    <tr>
        <td>{{   $key }}</td>
        <td>{{ $data->tenant_name}}</td>
        <td>{{ $data->property_name}}</td>
        <td>RM {{ $data->amount}}</td>
        <td>{{ \Carbon\Carbon::createFromFormat('Y-m-d', $data->payment_date)->format('m-d') }}</td>
        <td><span >{{ $data->status }}</span></td>                                       
        <td><a href="{{ route('view.email', $data->tenant->id) }}"   title="Send Email"> <i ></i> </a>
        </td>
    </tr>
@endforeach

Laravel way (Recommended)

Add cast to the eloquent model class.

protected $casts = [
    'payment_date' => 'date',
]; 

after that you can do this

@foreach ($alert as $key=>$data)
    <tr>
        <td>{{   $key }}</td>
        <td>{{ $data->tenant_name}}</td>
        <td>{{ $data->property_name}}</td>
        <td>RM {{ $data->amount}}</td>
        <td>{{ $data->payment_date->format('m-d') }}</td>
        <td><span >{{ $data->status }}</span></td>                                       
        <td><a href="{{ route('view.email', $data->tenant->id) }}"   title="Send Email"> <i ></i> </a>
        </td>
    </tr>
@endforeach
  • Related