Support the ongoing development of Laravel.io →
posted 9 years ago
Eloquent
Last updated 2 years ago.
0

short answer: No!

long answer: check this ooooooooooooooooooooooooout http://laravel.com/docs/4.2/templates#extending-blade

0

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

Najela,

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.

0

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.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

najela najela Joined 11 Apr 2014

Moderators

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

Your logo here?

Laravel.io

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

© 2024 Laravel.io - All rights reserved.