【问题标题】:Paginate in relationship and order by按关系和顺序分页
【发布时间】:2018-07-04 10:41:41
【问题描述】:

我有列表。列表有很多帖子。

我想获取一些列表的所有帖子,对帖子进行排序并分页。

我试试这个:

$list = List::whereId('1')->firstOrFail();
$posts = $feed->posts()->orderBy('id')->paginate(Request::input('per_page'))->appends(Request::input());

return compact('list','posts');

现在我可以获取列表和分页帖子,但 ordeBy 不起作用? 怎么订?或者有什么其他方法可以解决这个问题?

【问题讨论】:

    标签: php laravel laravel-5 pagination php-7


    【解决方案1】:
    $posts= Post::orderBy('id','desc');
    

    【讨论】:

      【解决方案2】:

      默认情况下,它按ASC 工作和排序。如果你想反转它,使用DESC

      ->orderBy('id', 'desc')
      

      或者:

      ->latest('id')
      

      【讨论】:

        【解决方案3】:
        ->orderBy('id','asc') for Ascending
        
        ->orderBy('id','desc') for Decending
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-07-14
          • 2015-06-10
          • 1970-01-01
          • 2022-01-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多