简体   繁体   中英

laravel format() function working for some blades only

I am damn confused about the format() function of laravel.In some blade format() works fine.But in user.blade works only when I add following function

public function getDates()
{
    return array('created_at', 'updated_at', 'deleted_at', 'last_login_at');
}

user.blade

<td>{{ $user->created_at->format('d/m/Y')}}</td>

and in my activity blade not working even getDates() function added.what's the reason behind that? Thanks in advance.

Error =Call to a member function format() on string (View: C:\\inetpub\\wwwroot\\odata\\resources\\views\\activity\\list.blade.php)

You don't need to override getDates() function. Just add columns to a $dates variable:

protected $dates = ['deleted_at', 'last_login_at'];

When you're doing this, Laravel will create Carbon instance for these dates, so you'll be able to use format() and other funtions.

It is giving you error because format() is function of Carbon.

Use below code in your blade.

 {{\Carbon\Carbon::parse($user->created_at)->format('d/m/Y')}}

To know more about Carbon visit http://carbon.nesbot.com/docs/ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM