问题
I'm trying to put the date order placed using Carbon.
my-order.blade.php
@foreach ($order->products as $product)
<div>{{ presentDate($order->created_at) }}</div>
<div>Total : {{ $product->price }} €</div>
<div>Quantity: {{ $product->pivot->quantity }}</div>
</div>
OrderProduct.php
use Carbon\Carbon;
function presentDate($date)
{
return Carbon::parse($date)->format('M d, Y');
}
Maybe I have to put the function in another place ?
Thanks for your help.
回答1:
You can leverage Laravel Accessors & Mutators
In your Model::class
public function getCreatedAtAttribute($date)
{
return Carbon::parse($date)->format('d-m-Y h:i:s');
}
In your blade, you can simply do this
<div>{{ $order->created_at }}</div>
来源:https://stackoverflow.com/questions/62044974/laravel-how-put-a-date-from-order-placed