I want to get the latest timestamp. For some reason, when I try to use sortBy
or orderBy
, it doesn't work. Can anyone help me?
Here is the codes that I tried:
public function getTest($id) {
$data = test::where('user_id', $id)->sortByDesc('created_at')->get();
// I also tried doing this
$data = test::where('user_id', $id)->orderBy('created_at', 'desc')->get();
if(count($data)>0) {
return view('test', compact('data'));
}
else {
return view('test');
}
}
Try:
$data = test::where('user_id', $id)->orderBy('created_at', 'desc')->first();
This will order the rows in table by created_at
.
Hope this helps you!