0
votes

I have a column with datatype of date which is date mutated. I date mutated it so that Laravel will convert it to a Carbon instance and I can use it easily at other places, where I need to convert it into Carbon instance. I am using Model binding in the edit form. As the field is date mutated, on the edit form it appears as "2015-07-29 00:00:00". I need it to be in this format instead: "2015-07-29".

I can't use accessor, as I need it as a Carbon instance at many other places.

I can't explicitly pass value after converting, as I am using the input inside a form partial and I also use it for create.

My workaround is the following:

I am sending a flag while including the view partial in the edit page and using it a condition to have two different code for create and edit.

@if (isset($edit))
    {!! Form::text('eta', $order->eta->format('Y-m-d'), ['class' => 'form-control', 'required']) !!}
@else
    {!! Form::text('eta', null, ['class' => 'form-control', 'required']) !!}
@endif

Is there a better way?

1

1 Answers

0
votes

I think you can avoid from @if and $edit as,

{!! Form::text('eta', ( $order->eta ? $order->eta->format('Y-m-d') : null ) , ['class' => 'form-control', 'required']) !!}