Background output of "View Composer"

19.04.2021 | 876 | SQL

Background output of dynamic information on all pages

View Composers will be useful when some data is shared for many or all pages. Examples: navigation or a block with popular articles, etc. It is for such cases that Laravel provides an elegant solution. Also, a good point for performance is that without visual output, the compositor code also does not work. Let's create a template for the output:
Let's add the code for the output of articles
@foreach ($articles as $article)
{{ $article->active_from }}
{{ $article->code }}
{{ $article->name }}
Let's call the template in the general website template (or any other):
Add the provider to the "providers" array of the configuration file "config/app.php ":

'providers' => [
Creating a provider:
Adding our new View Composer to it:
namespace AppProviders;

use IlluminateSupportFacadesView;
use IlluminateSupportServiceProvider;

class ComposerServiceProvider extends ServiceProvider
public function register()

public function boot()
Creating View Composers:
We add the code for getting articles to it:
namespace AppHttpViewComposers;

use App;
use AppModelsArticle;
use IlluminateViewView;

class ShowSideBar
public function compose(View $view)
$articles = Article::where('active', 1)

return $view->with(['articles' => $articles]);
Now on every page of the site you should see a list of articles and you don't need to repeat getting it in the page controllers.

← Back

Comments (0)