I am newbie to laravel. Is it necessary to create model for database operations in laravel 5. I am asking for query builder not for Eloquent ORM. In some tutorials i saw they are performing database queries in controllers that's why i am asking whether it's ok or it's better to create any models ?
1
votes
One table, one model. So if you intend on having more than one table, which you probably will, you'd need a model for said table. It doesn't matter if you're using Eloquent or the Query builder. And yes, you can perform queries in the controllers.
- Andrei
if i can perform queries in controllers, why should i need model or what is the necessary to create model? @Andrew
- prudhvi259
That depends mate, means if you have some functions which are common throughout project, make common model or controller, if it is table specific, then its better to write in its relative model
- Rahul
@prudhvi259 You can write the entire app in a single class file. But that's not the correct way to write a good structured, readable & extendable code. And that's the reason these frameworks comes with MVC structure. So take a look on MVC pattern and also take on SOLID programming practices.
- Peeyush
1 Answers
2
votes
That is a kind of philosophical question about Object programming.
Personal opinion follow:
In Laravel you can query directly the tables with the query builder, you dont need any Eloquent Model.
For simple cases, i.e a small api, the query builder can be enough.
In more complex applications, when you have tables with relations and a bit of logic an ORM and/or some Repositories classes could be the way to go, not because they are a must but because they can help you to keep thinks organized.