将orderBy('created_at','desc')添加到我的帐户

时间:2018-07-21 11:25:20

标签: php laravel sql-order-by

我想为自己帐户中发布的文章创建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,


    ]);


}

1 个答案:

答案 0 :(得分:2)

假设在用户模型上该关系是正确的:

'articles' => $user->articles()->orderByDesc('created_at')->paginate()