title: How to share variables to all controllers and views in Laravel?
body: <p>I have a Laravel 5.5 project where I would need to share some variables throughout all controllers and all views with the option to have some global defaults and then those defaults could be edited within the various controller methods, and in the end the views would show those updated variables.</p>
  
  <p>For example:</p>
  
  <pre><code>$test = [1, 2, 3];
  </code></pre>
  
  <p>in <code>fooController.php</code> BarMethod I would say:</p>
  
  <pre><code>$test[] = 4;
  </code></pre>
  
  <p>Then the view file that is accompanied with that BarMethod would see <code>[1, 2, 3, 4]</code>.</p>
  
  <p>But if I said something else in <code>testController.php</code> BazMethod:</p>
  
  <pre><code>$test = array_merge($test, [100, 1000, 10000]);
  </code></pre>
  
  <p>Then my other view that belongs to this method would see <code>[1, 2, 3, 100, 1000, 10000]</code>.</p>
  
  <p>Any ideas are welcome.</p>
  
  <p>Thanks.</p>
  
  <hr>
  
  <p>Okay, so to clarify: I am talking about multiple requests, the 1st request would go to <code>fooController</code>, the 2nd one to <code>testController</code>, the nth one to some <code>nthController</code>. In all cases there would be the initial <code>$test</code> variable with its default value, then all the requested methods in the controllers could alter the default initial variable and then send it to the view.</p>
  
  <p>All this I can achieve for example by having the default values of <code>$test</code> set in a <code>baseController</code>, then update the values in either of the methods, but my real issue is that I need to specify the variable among the list of variables to send to the view in all methods. </p>
  
  <p>So what I'd need is a way automatically send that updated <code>$test</code> variable to all views. I was looking at service providers, but it seemed to me that a service provider would do its job before editing the values, so the initial value would show up in the view, not the updated value.</p>
  
  <p>I honestly don't think sessions could help in this situation.</p>