I need to OrderBy a column with collection.
I need to orderBy(updated_at, 'desc')
all posts which owned by current logged user.
以下是我的代码:
$posts = auth()->user()->posts->sortByDesc('updated_at');
以下是用户模型:
class User extends Authenticatable
{
public function posts()
{
return $this->hasMany(Post::class);
}
}
It doesn't return any errors also doesn't sort !
任何帮助都将不胜感激.
P.S:
我知道我可以通过以下方式实现这一点:
$posts = Post::where('user_id', auth()->user()->id)->orderBy('updated_at', 'desc')->get();
但我想对Collection 做同样的事情.