简体   繁体   English

在Laravel中构建REST API

[英]Building a REST API in Laravel

I'm going from no-framework development to my first contact with Laravel (using version 5.1) and I'm a bit overwhelmed with the amount of stuffs to handle (migrations, seeds, hateoas, transformers, unit testing, error handling, etc). 我将从无框架开发转到我与Laravel的第一次接触(使用版本5.1),我有点不知所措,需要处理的东西(迁移,种子,hateoas,变换器,单元测试,错误处理等) )。

I'd like to get some help at the return of the show method portion of it (following REST concepts). 我想在返回show method部分时获得一些帮助(遵循REST概念)。 I have the following database structure: 我有以下数据库结构:

companies -> users -> tickets -> interactions -> attachments 公司 - >用户 - >门票 - >互动 - >附件

在此输入图像描述

Now, when listing a specific ticket (like GET /customers/5/tickets/3 ), the screen needs to show all interactions for that ticket, along with all information in the ticket table (sort of like a page header) and the name of each user that created the specific interaction. 现在,当列出特定故障单(如GET /customers/5/tickets/3 )时,屏幕需要显示该故障单的所有交互,以及故障单表中的所有信息(有点像页眉)和名称创建特定交互的每个用户。

Thinking in terms of relationship, should I return all that data from the single call or should the information from the tickets table, for instance, be persisted in the front-end (from the index method call)? 考虑到关系,我应该从单个调用中返回所有数据,还是应该将票证表中的信息保留在前端(来自index method调用)? Even if I should persist the ticket information (header of the page), I am still left with interaction join users + N attachments for each interaction. 即使我应该保留票证信息(页面标题),我仍然会为每次互动留下interaction join users + N个附件。 What are the options to approach this? 有什么方法可以解决这个问题?

I decided to open this thread because I was trying to Unit Test this call and I couldn't decide whether I should seed the database to have all the information necessary for it to work. 我决定打开这个线程因为我试图对这个调用进行单元测试,我无法决定是否应该为数据库设置种子以获得所需的所有信息。

Edit 编辑

To better clarify my request, I'm not in doubt on how to handle the database communication, I already have my models set extending Eloquent class and I defined the hasMany, belongsTo, etc data. 为了更好地澄清我的请求,我对如何处理数据库通信毫无疑问,我已经将模型设置为扩展Eloquent类,并且我定义了hasMany,belongsTo等数据。

namespace App\Models;

class Company extends Model {

    /**
     * Get the users from the company.
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function users() {
        return $this->hasMany('App\Models\User');
    }
}

To not make the post bigger than it needs to be, I'll settle with just providing the Company model. 为了不让这个职位变得更大,我只会提供公司模式。 I hope you all get the concept that I have Company , User , Ticket , Interaction and Attachment models all set and ready to use. 我希望你们都能得到我所拥有的公司用户票证互动附件模型的概念,并且随时可以使用。

Edit 2 编辑2

The question is around the practice in handling it. 问题在于处理它的做法。 Should the GET /customers/5/tickets/3 return the ticket record, all interactions for it, the user for each interaction, all the attachments for each interactions? GET /customers/5/tickets/3是否应该返回票据记录,所有交互 ,每次交互的用户 ,每次交互的所有附件 Isn't that over responsibility for a single endpoint? 对于单个端点,这不是责任吗? Like I mentioned above, I think I can demand from the front-end to persist the ticket information from when the front-end listed all tickets, that takes out the burden of the ticket record, but it's still a TicketController and there are still a lot of data to be loaded. 就像我上面提到的那样,我想我可以从前端要求保留票据信息,从前端列出所有票据时,它取消了票据记录的负担,但它仍然是TicketController,但仍然存在要加载的数据很多。 I'm seeking for advice on handling this much information because I never did it before and I would like to minimize the amount of things I'll regret for doing. 我正在寻求处理这些信息的建议,因为我之前从未这样做过,我想尽量减少我会后悔做的事情。

I think you should return all the information your API consumers will need to display one ticket entry. 我认为您应该返回API消费者显示一个票据条目所需的所有信息。 I've worked on projects where there were query parameters to tell the server which related models to include, and others where subsequent AJAX calls were needed to get the associated data, but I think that's needlessly complex unless the interaction data is huge or very rarely needed. 我曾经在那些有查询参数的项目上工作,告诉服务器要包含哪些相关模型,以及需要后续AJAX调用来获取相关数据的其他项目,但我认为除非交互数据很大或非常罕见,否则不必要地复杂需要。

I would recommend creating view partials for each model, so that you have flexibility in case one large view doesn't work out. 我建议为每个模型创建视图部分,以便在一个大视图无效的情况下具有灵活性。

Solving the N+1 query problem you mentioned is trivial though. 解决你提到的N + 1查询问题虽然微不足道。 Here is a perfect situation for eager loading . 这是一个渴望加载的完美情况。

Try something like this: $tickets = Tickets::with('interactions','user')->get() 尝试这样的事情: $tickets = Tickets::with('interactions','user')->get()

That will load all associated data in one query. 这将在一个查询中加载所有相关数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM