简体   繁体   English

laravel 每个表都需要一个模型和控制器吗?

[英]laravel do I need a Model and Controller for every single table?

I know this is a duplicate question but i think it will help others because there are a lot of similar apps that have these kind of table relationships:我知道这是一个重复的问题,但我认为它会帮助其他人,因为有很多类似的应用程序具有这种表关系:

在此处输入图片说明

So the question is what would be the optimal solution for all relationships in this schema using the Eloquent?所以问题是,使用 Eloquent 的这个模式中所有关系的最佳解决方案是什么? How many Models and Controllers to make?要制作多少模型和控制器?

First of all, you need to understand that not all tables in a database represent an entity.首先,您需要了解并非数据库中的所有表都代表一个实体。

For example, tables like users , posts , comments are entities.例如,像userspostscomments这样的表就是实体。 Whereas posts_users , comments_posts are not: they are here for technical reason, to materialize the relation between 2 entities.posts_userscomments_posts不是:它们出于技术原因在这里,以实现 2 个实体之间的关系。

Only entities need a model: it makes no sense to have a model for a relation table.只有实体需要模型:为关系表建立模型是没有意义的。

Even if a table holds information like date_created , it does not make it an entity.即使一个表包含诸如date_created信息,它也不会使它成为一个实体。 This is just a data related to the relation.这只是与关系相关的数据。 For example, the table users_roles may have a column named date_assigned , to know when a given user was assigned a given role.例如,表users_roles可能有一个名为date_assigned的列,以了解给定用户何时被分配给定角色。 It's not entitity for all that.它不是所有这些的实体。

Second, you need to understand what a controller is for.其次,您需要了解控制器的用途。 The role of a controller is to handle a request and provide a result.控制器的作用是处理请求并提供结果。 Result can be a view, an error (HTTP 404), or just the fact that an action has been successfully done.结果可以是一个视图、一个错误 (HTTP 404),或者只是一个操作已经成功完成的事实。

You must also make the difference between the class called Controller (or any child that extends this base class) and an actual controller.您还必须区分名为Controller (或扩展此基类的任何子类)和实际控制器的类。 The actual controller is the code that will handle the request.实际控制器是处理请求的代码。 A Controller class can have more than one method to handle requests.一个 Controller 类可以有多个方法来处理请求。

It's all a question of organization: generally, Controller classes are used to group methods within the same scope: user login, logout, subscription, password reminder are all the same scope.这都是一个组织问题:一般情况下,Controller类用于对同一范围内的方法进行分组:用户登录、注销、订阅、密码提醒都属于同一范围。 All these controllers could be in different classes or functions.所有这些控制器都可以在不同的类或功能中。 It does not matter.没关系。 Each method is a controller.每个方法都是一个控制器。 They are grouped in the same class because they have the same needs (check user is logged in, to know if login is required, if subscription page can be displayed, etc.) and they are in the same scope, which just make sense when you think of it.它们被归为同一个类,因为它们具有相同的需求(检查用户是否登录,了解是否需要登录,是否可以显示订阅页面等)并且它们在相同的范围内,这才有意义你想想。 That's just logical: you know where to search when you need to change something about user identification (even if you are new on the project).这是合乎逻辑的:当您需要更改有关用户标识的某些内容时(即使您是该项目的新手),您知道在哪里搜索。

So you need a model for these entities:所以你需要这些实体的模型:

  • User用户
  • Offer优惠
  • Invoice发票
  • Category类别

The controllers you'll need depends on what you want/need to do with this data.您需要的控制器取决于您想要/需要对这些数据做什么。 There's no ready-to-use answer to this part of your question.您问题的这一部分没有现成的答案。

Use a Controller class for:使用 Controller 类:

  • user authentication (if you need some)用户身份验证(如果您需要一些)
  • user management (backoffice)用户管理(后台)
  • invoice management (edit, mark paid, list of late payments, etc.)发票管理(编辑、标记已付、逾期付款清单等)
  • categories management (create, edit, delete)类别管理(创建、编辑、删除)
  • offers management提供管理

But depending on your application, you may need more.但根据您的应用程序,您可能需要更多。 Only you can really say.真的只有你能说。 There's no bad answer to this question: if you think some controllers should be separated for better organization, do it.这个问题没有错误的答案:如果您认为应该将某些控制器分开以便更好地组织,那就去做吧。 If you think you should group 2 or more, do it.如果您认为应该将 2 组或更多组分组,请执行。 There's no rule.没有规则。 You have to keep your code clear and well organized.您必须保持代码清晰且组织良好。 It must suit your needs.它必须适合您的需求。 That's all.仅此而已。

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

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