Laravel : How put a date from order placed

陌路散爱 提交于 2021-01-28 09:06:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!