我想为自己帐户中发布的文章创建orderBy ('created_at', 'desc')
和paginate
,这是我的代码。
MyaccountController
public function viewProfile($username) {
if($username) {
$user = User::where('username', $username)->firstOrFail();
} else {
$user = User::find(Auth::user()->id);
}
return view('site.user.account', [
'user' => $user,
'events' => $user->events,
'articles' => $user->articles,
]);
}
答案 0 :(得分:2)
假设在用户模型上该关系是正确的:
'articles' => $user->articles()->orderByDesc('created_at')->paginate()