简体   繁体   English

模型在Laravel 5

[英]Models in Laravel 5

I'm doing a web app here using Laravel + AngularJS and I have a question.Do I need a model for each table that I have in my database? 我正在使用Laravel + AngularJS在做一个Web应用程序,我有一个问题,我是否需要为数据库中的每个表建立模型? There are 87 tables in my database and I need to query all of them according to with the input that the User wants. 我的数据库中有87个表,我需要根据用户想要的输入来查询所有表。

I just want to make sure with all tables must have a model file or if just one is enough. 我只想确保所有表都必须有一个模型文件,或者只要一个就足够了。

It depends on how you'd like to handle queries. 这取决于您要如何处理查询。

If you'd like to use Eloquent ORM, you need model classes to handle objects and relationships. 如果您想使用Eloquent ORM,则需要模型类来处理对象和关系。 That is a model for a table, except intermediate relationship tables, which may be accessed through pivot attribute. 除中间关系表外,这是表的模型,可以通过数据透视表属性访问这些表。

Raw SQL queries are also supported. 还支持原始SQL查询。 You don't really need model classes for them, as each result within the result array will be a PHP StdClass object. 您实际上并不需要模型类,因为结果数组中的每个结果都是一个PHP StdClass对象。 You need to write raw SQL though. 不过,您需要编写原始SQL。

See Laravel documentation . 请参阅Laravel 文档

There are 2 ways by which you can access your DB tables: 有两种方法可以访问数据库表:

  1. Eloquent ORM (dependent on Models) 雄辩的ORM (取决于型号)
  2. DB Facade Query Builder (independent on Models) DB Facade 查询生成器 (独立于模型)

Former, is more clean and best approach to perform DB query and related task, whereas latter is not clean, and it is going to be difficult for you to manage large application, as you told there are 80+ tables in your application. 前者是执行数据库查询和相关任务的更干净,最佳方法,而后者则不干净,因为您告知应用程序中有80多个表,这将使您难以管理大型应用程序。

Also, if you're using Eloquent way, then it's also a better to have a base model, which will have common code which you can inherit in child models. 另外,如果您使用的是Eloquent方式,那么拥有一个基本模型也更好,该模型将具有可以在子模型中继承的通用代码。 Like if you want to store "user id" who did some DB changes, then in the boot function, you can write Auth::id() and assign that value to changed_by field on your table. 就像如果您要存储进行过一些数据库更改的“用户ID”一样,那么在启动功能中,您可以编写Auth::id()并将该值分配给表上的changed_by字段。

In DB Facade way, you've to hard code table name every time you're performing DB operation, and which leads to inconsistency when you found that you've to change the name of the table, it's a rare scenario still it'll be difficult to manage if in a file there are multiple tables DB operation going on. 用DB Facade方式,每次执行DB操作时都要对表名进行硬编码,这会导致在发现必须更改表名时导致不一致,这是一种罕见的情况,仍然会如果文件中有多个表正在进行DB操作,将很难管理。 There are options like, creating a global table name variable which can be accessed to perform DB operation. 有一些选项,例如,创建全局表名变量,可以访问该变量来执行数据库操作。

Conclusion: Yes, creating 80+ model for implementing Eloquent way is painful, but for a short term, as the application grows it will be easy for you to manage, it will be good for other developer if they start working on it, as it will give a overview of DB and it will improves code readability. 结论:是的,为实现Eloquent方式创建80+模型是很痛苦的,但是从短期来看,随着应用程序的增长,您将很容易管理它,对于其他开发人员,如果他们开始从事它,那将是一件好事将概述数据库,并提高代码的可读性。

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

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