简体   繁体   中英

Laravel, Database design for multiple model in one row

I'm building an application that shows a list of modules like 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.

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. 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.

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

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. 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. 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. Each of these models would have a module_id column, to link them to the Modules they belong to. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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