Search code examples
laravelroutesnaming

Should all models be represented in DB, or better to make only a controller if modell is not represented in DB?


I wonder about the route leading to the welcome-page in my project. You reach it with the '/'-route .

Where should the method handling this route be and what is the correct structure behind it?

I get two ideas spontaneously:

  1. Make a model named something like Home and put the method in HomeController

    (Con: I thought all modells should be represented in DB?)

...

  1. Make only a controller named HomeController, no model

    (Con: Feels wrong to have a controller for a modell that does not exist?)

What is correct?


Solution

  • I can't comment so I'll leave it here.

    Both Model and Controller are just Class. If you take a look at your HomeController and your User.php model you'll see they extend Controller and Model Classes, respectively.

    As many said there is no need to have 1 to 1 relationship between Controllers and Models.

    Invest some time to learn basics of OOP and you will see that you can have as much classes as you want, and there are many ways to relate them. Some of the Classes are Controllers, some are Models and some are something completely else, like Exceptions. But at all times keep in mind that, in the end, they are just Classes implementing some interface, using some traits, and extending other Classes.

    It's worth to do this at the start of your learning process. It will steepen your learning curve a bit but in the long run it's well worth it.