douqujin2767 2016-05-14 22:32
浏览 71

Laravel网站循环导航订单

I am currently doing my college thesis with Laravel 5.2, it is a Courses website similar to www.udacity.com.

I have the following models:

  • Course
  • Chapter
  • Quiz

A course can have many chapters and a chapter may or may not have many quizzes. I have specified these relationships in each model.

I want to follow the same viewing order as Udacity has, where the user only has a "previous" and "next" button to cycle through the course. So, if I am viewing the course intro, the only available button is "next", that would take me to the first chapter in the course, at that moment I would have both buttons, the previous one would take me back to the course intro and the next one would take me to either the chapter's first quiz or to the next chapter if it does not have any quizzes.

Udacity Order Example

Currently, I have an awful previous and next button logic full of if statements to determine if there is a next/previous chapter/quiz or not.

What would be the best practice form of achieving this?

Thanks for your help!

  • 写回答

1条回答 默认 最新

  • doubo4824 2016-05-14 23:07
    关注

    Why don't you use Laravel's pagination?
    https://laravel.com/docs/5.1/pagination

    $chapters = App\Chapter::paginate(1);
    or something like
    $chapters = App\Course::find(1)->chapters()->paginate(1);
    

    should give you 1 chapter per page.
    Then you can use helpers methods to get previous and next links in a view, rather than displaying all links:

    $results->hasMorePages()
    $results->nextPageUrl()
    $results->previousPageUrl()
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大