Php 使用Laravel对一对多(多态)关系进行排序
因此,我一直在尝试使用多态关系为我的博客构建一个新的评论系统。这一切都很好,但我有麻烦排序的意见 例如,在这里,我通过控制器获得所需的内容并传递到视图Php 使用Laravel对一对多(多态)关系进行排序,php,mysql,laravel,Php,Mysql,Laravel,因此,我一直在尝试使用多态关系为我的博客构建一个新的评论系统。这一切都很好,但我有麻烦排序的意见 例如,在这里,我通过控制器获得所需的内容并传递到视图 $post = BlogPosts::with('BlogCategories', 'Users', 'BlogTags', 'Comments') ->where('slug', $slug) ->first(); return view('blog.show', compact('post')); 所有这些都很
$post = BlogPosts::with('BlogCategories', 'Users', 'BlogTags', 'Comments')
->where('slug', $slug)
->first();
return view('blog.show', compact('post'));
所有这些都很好,并返回带有各种关系的博客帖子,包括评论
我正在努力解决的是,我想对结果进行排序,以便贴在帖子上的评论按降序显示。事实上,这比我想象的要容易,你可以简单地在blade模板中对关系进行排序,如下所示
@foreach( $post->comments->sortByDesc('created_at') as $comment)
....
@endforeach
确实有道理。事实上,这比我想象的要容易,你可以简单地在blade模板中对关系进行排序,如下所示
@foreach( $post->comments->sortByDesc('created_at') as $comment)
....
@endforeach
这很有道理