ran*_*onk 1 laravel laravel-5.3
大家好我想在diffForHumans()中显示日期
在我使用的线程控制器中
$thread = DB::table('threads')
->join('users', 'users.id', '=', 'threads.user_id')
->select('users.*', 'threads.*')
->orderBy('threads.id', 'DESC')
->get(); return view('thread.index', compact('thread'));
Run Code Online (Sandbox Code Playgroud)
在我看来
{{ $item->created_at->diffForHumans() }}
Run Code Online (Sandbox Code Playgroud)
但我发现错误
Call to a member function diffForHumans() on string
Run Code Online (Sandbox Code Playgroud)
在此之前我使用$thread = Thread::paginate(3);和方法diffForHumans()工作我的问题是什么?
Laravel DB门面不返回雄辩的对象,因此,created_at和updated_at类型不是DateTime.Eloquent对象的datetime实例返回Carbon的实例.
解
因为你created_at是一个字符串,你将不得不解析它以使Carbon工作.
{{ \Carbon\Carbon::parse($item->created_at)->diffForHumans() }}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
972 次 |
| 最近记录: |