Support the ongoing development of →
posted 9 years ago
Last updated 2 years ago.

short answer: No!

long answer: check this ooooooooooooooooooooooooout


No, the right way would be to wrap the query in a repository class (Repository pattern). The wrapped function in the repository class will make the actual call to the model. Also the repository class should not be used in the view, but in the controller instead.

You can use dependency injection to initiate a repository instance in the controller's constructor. In the action function (e.g. showIndex, showContact) you can call the instance and bind the result to the view.

Last updated 9 years ago.


Ideally, this query would be executed prior to being passed to the view. Putting this logic in the controller would get it closer to being better, but in order to be "best practice" you need to segregate this logic into a class that is solely responsible for your data queries. As Chris said above, such a class would be something like a Repository class, which could be injected into your controller.

Then, as you return your view from your controller, the second parameter (or model passed to the view) would contain the data that you want to present on your view. The view should be dumb and only render what it is passed from the controller.

Think of your controller as being the portion of your application that is responsible for serving up the view (and the data that the view needs), and the view as having the responsibility of rendering what it is provided from the controller.


A good pattern to read up on is MVC which stands for Models, Views and Controllers.

It's a design pattern that focuses of Separation of Concerns, where you separate the different concerns of your applications into different sections. The data retrieval is the Model (typically a database but not necessarily), a View (the Blade templates) and the Controller.

You can go beyond just MVC and separate things further to include Repositories, Events & Commands for a better separation.

The reason you might want to use Repositories (which are just wrappers around your Model) is to abstract the actual retrieval and persistence of your data from your Controller, allowing you to change from using a database to another form of strange without having to update the code in your controllers.


Sign in to participate in this thread!


Your banner here too?

najela najela Joined 11 Apr 2014


We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 - All rights reserved.