Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用Laravel对一对多(多态)关系进行排序_Php_Mysql_Laravel - Fatal编程技术网

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
这很有道理