laravel关联排序时默认是按asc升序排序的,怎么设置某个字段按desc降序排序?另外就是,关联模式一对多(belongsTo)怎么分页呢?
微信里点“发现”,扫一下
二维码便可将本文分享至朋友圈。
问题1,比如文章和评论。Post和Comment。
Post
Comment
public function comments() { return $this->hasMany(App\Comment::class)->orderBy('column', 'desc'); }
问题2:比如文章和分类。Post和Category。
Category
//Category.php public function posts() { return $this->hasMany(App\Post::class); }
使用时:
$categroy->posts()->paginate(15);
谢谢,但是在模板中怎么调用分页链接?比如$categroy->posts()->links() 尝试了不行
可以了,就在$categroy->posts()->paginate(15)后面直接加links()