Issue
<?php
namespace project1\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use View;
class IndexController extends Controller
{
public function index()
{
$users = DB::table('users')->orderBy('created_at','desc')->limit(5)->get();
return View::make('pages.dashboard.dashboard',['users' => $users]);
}
}
Is it possible to echo the Time Ago in my View ( pages.dashboard.dashboard ) Something Like,
{{ $user->created_at->diffForHumans() }}
Expecting output like,
1 min ago
Solution
First, try an eloquent model.
$users = App\User::orderBy('created_at','desc')->limit(5)->get();
in view
@foreach($users as $user) {{$user->created_at->diffForHumans()}} @endforeach
In Terms for your case
$users = DB::table('users')->orderBy('created_at','desc')->limit(5)->get();
@foreach($users as $user)
{{ Carbon\Carbon::parse($user->created_at)->diffForHumans()}}
@endforeach
Explanation
The eloquent model automatically get casts to Carbon instance and you can use an all methods
In the case of DB query date (created_at) is not get parse so we have to parse manually.
Answered By - im-sunil
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.