Laravel Nova 4 format date in BelongsTo field

25 Views Asked by At

I kindly need your support to understand how to apply the same date format, as indicated in the original resource, to the belongsTo field in Laravel Nova 4.

Resource 1

public function fields(Request $request)
{
    return [
        ID::make('id')->sortable(),

        Date::make('Shipment Eta Date')
            ->rules('required', 'date')
            ->placeholder('Shipment Eta Date')
            ->displayUsing(fn ($value) => $value ? $value->format('D d/m/Y') : ''),

        HasMany::make('ShipmentInfos', 'shipmentInfos'),
    ];
}

Resource 2

public function fields(Request $request)
{
    return [
        ID::make('id')->sortable(),

        Date::make('Shipment Sent Date')
            ->rules('required', 'date')
            ->placeholder('Shipment Sent Date')
            ->displayUsing(fn ($value) => $value ? $value->format('D d/m/Y') : ''),

        HasMany::make('ShipmentInfos', 'shipmentInfos'),
    ];

Resource 3

BelongsTo::make('ShipmentSentDate', 'shipmentSentDate'),
BelongsTo::make('ShipmentEtaDate', 'shipmentEtaDate'),

I tried this, but it doesn't work:

BelongsTo::make('ShipmentSentDate', 'shipmentSentDate')
    ->displayUsing(fn ($value) => $value ? $value->format('D d/m/Y') : ''),
            
BelongsTo::make('ShipmentEtaDate', 'shipmentEtaDate')
    ->displayUsing(fn ($value) => $value ? $value->format('D d/m/Y') : ''),
1

There are 1 best solutions below

0
Antonio Graziani On

I apologize for the inconvenience, and I would like to thank @Harris and @Karl Hill. Here's the solution:

BelongsTo::make('ShipmentSentDate', 'shipmentSentDate')
    ->displayUsing(function ($shipmentSentDate) {
        return Carbon::now()->format('Y-m-d');
    }),

Thank you.