View composer runs multiple times, how to reduce to 1

Unfortunately no, there is no way to make it run once, due to the way View Composers are implemented. The Illuminate\View\View::renderContents() method is responsible for calling the composer bound to a view, and since any form of view rendering (Blade template inheritance or simple @include statements) executes that method, it means that when any view is rendered any composer bound to it gets triggered.

Since in your case you use a * wildcard to bind all views, if your page renders ten views, the composer will get executed ten times. However your approach looks like a good solution to solve this shortcoming.

Tags:

Php

Laravel