根据上一个项目的ID获取数组中的下一个项目 [英] Getting the next item in an array based on the ID of the previous item

查看:131
本文介绍了根据上一个项目的ID获取数组中的下一个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一些背景信息

我正在使用AngularJS作为前端框架,并使用Laravel作为与前端进行通信的API.

I am using AngularJS as a frontend framework and Laravel for an API for the front end to communicate with.

问题

对于此应用程序,我有一个部分",该部分在视图中具有下一个/上一个"按钮,当单击下一个按钮时,将使用当前部分ID作为参数进行API调用.因此,我希望根据所使用的端点(单击了哪个按钮)来掌握数组中的下一个或上一个项目.

For this app I have a 'section' which has a next/prev button in the view, when the next is clicked, an API call is made with the current section ID as a parameter. From this I was hoping to get hold of the next or previous item in the array depending on the endpoint I use (which button was clicked).

我将如何在PHP/Laravel中进行此操作?是否有可能或者我应该在考虑我的app/api的结构?

How would I go about this in PHP/Laravel, is it possible or should I be re thinking about the structure of my app/api?

我现在拥有的东西

所以目前,我所拥有的只是一个部分"收藏:

So at the moment, I all I have is a 'Section' collection:

$sections = Section::orderBy('order', 'DESC')->orderBy('id', 'ASC')->get();

推荐答案

您可以使用分页

$sections = Section::orderBy('order', 'DESC')->orderBy('id', 'ASC')->paginate(2);
return response()->json($sections );

然后您会得到类似(我转换为json)

Then you will get something like (I convert into json)

{
    "total": 17,
    "per_page": 2,
    "current_page": 1,
    "last_page": 9,
    "next_page_url": "{{URL}}?page=2",
    "prev_page_url": null,
    "from": 1,
    "to": 2,
    "data": [
        {
            "id": 40,
            "order":1
        },
        {
            "id": 41,
            "order":2
        },
     ]
 }

现在使用网址格式"next_page_url"或"prev_page_url"

Now use the url form "next_page_url" or "prev_page_url"

这篇关于根据上一个项目的ID获取数组中的下一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆