简体   繁体   English

Laravel,多行模型数据库设计

[英]Laravel, Database design for multiple model in one row

I'm building an application that shows a list of modules like Udemy. 我正在构建一个显示诸如Udemy之类的模块列表的应用程序。 Below is the simplified example of what I'm trying to achieve. 下面是我要实现的简化示例。

(This is not a database table, this is the interface illustration) (这不是数据库表,这是界面图)

No   Module name               Type
1    Introduction               Video
2    What is Composer?          Video
3    Model View Controller      Video
4    Blade Templating Engine    Video
5    Quiz - First Quiz          Quiz

As you can see, this is what you would usually see on online education websites such as Udemy or Lynda. 如您所见,这通常是在在线教育网站(如Udemy或Lynda)上看到的。

It shows a list of modules and each module can be a video module or a quiz module. 它显示了模块列表,每个模块可以是视频模块或测验模块。

A video module has a video URL column in the database. 视频模块在数据库中具有视频URL列。 While a quiz module is completely different than the video module. 测验模块与视频模块完全不同。 It will have questions and answers. 它将有问题和答案。

We can order the modules in the CMS, therefore, each module will have an order column in the database. 我们可以在CMS中订购模块,因此,每个模块在数据库中都会有一个订购列。

The question is: 问题是:

How we can model Module -> Video, Module -> Quiz in the database? 我们如何在数据库中对模块->视频,模块->测验建模? What kind of table relations do I have to build? 我必须建立什么样的表关系?

You can refer to Laravel Eloquent relationship: link 您可以参考Laravel雄辩的关系: 链接

By using that, you will need three tables: modules, quizes and videos 使用此功能,您将需要三个表格:模块,测验和视频

What you want to do is, have one db object/model to represent both quiz and video, because based on your explanation, they are to be seen as the-same thing, ie they are both modules. 您想要做的是,有一个数据库对象/模型来表示测验和视频,因为根据您的解释,它们将被视为同一事物,即它们都是模块。 The only difference, is in the type. 唯一的区别在于类型。 This implies you need a 'type' column in the DB table, in order to indicate the type of each module. 这意味着您需要在DB表中有一个“类型”列,以指示每个模块的类型。 You would also need a URL column, which should be (nullable), ie optional, as it will only be needed if the module is a video type, as you explained. 您还需要一个URL列,该列应为(可为空),即可选,因为如您所解释的,只有在模块是视频类型时才需要。 With regards to having questions and answers, this would need to be a related model, because questions/answers will probably need to store their own data (eg created_at, etc) Thus, I would say you may need a Question Model(DB object) as well as an Answer one. 关于问题和答案,这将需要是一个相关模型,因为问题/答案可能需要存储自己的数据(例如,created_at等)。因此,我想说您可能需要一个问题模型(DB对象)以及答案一 Each of these models would have a module_id column, to link them to the Modules they belong to. 这些模型中的每一个都有一个module_id列,以将它们链接到它们所属的模块。 You eloquent model relationships would look like this 您雄辩的模型关系如下所示

 <!--module model--> <?php namespace App; use Illuminate\\Database\\Eloquent\\Model; class Module extends Model { public function questions(){ return $this->hasMany('App\\Question'); } public function answers(){ return $this->hasMany('App\\Answer'); } } <!--Question model--> <?php namespace App; use Illuminate\\Database\\Eloquent\\Model; class Question extends Model { public function module(){ return $this->belongsTo('App\\Module'); } } <!-- Answer Model --> <?php namespace App; use Illuminate\\Database\\Eloquent\\Model; class Answer extends Model { public function module(){ return $this->belongsTo('App\\Module'); } } 

That being said however, I don't think this is the best way to approach this. 话虽这么说,但我认为这不是解决此问题的最佳方法。 If what you are trying to do is, have a Course, which has several modules, and that also could have a quiz or quizzes, then what you should really be doing is relating both the a modules model, and the quiz(es) model, directly to a Course model. 如果您想做的是开设一门课程,该课程包含多个模块,并且还可能包含一个测验,那么您真正应该做的是将模块模型与测验模型相关联,直接转换为课程模型。 So a Course would have a one to many relationship with modules, and a one to many relationship with quizzes. 因此,课程与模块具有一对多的关系,与测验具有一对多的关系。 That way, no need for a 'type' column, and questions and answers would only be related to quizzes and not to modules, which to me makes more sense. 这样,就不需要“类型”列,问题和答案仅与测验有关,而与模块无关,这对我来说更有意义。

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

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