title: New to Laravel - How to pass model data to a blade view?
body: <p>Ok, I am totally re-writing this question, now that I am a bit more familiar with larval.</p>
  
  <p>Here is my situation: I have a guitar lessons site based on larval 5.2.36, where each lesson belongs to a category, and within a lesson are several exercises. An exercise table does not have a category id as it is linked to a lesson which has a category. </p>
  
  <p><strong>Goal</strong> What I am trying to figure out is how to pass the category of the currently displayed lesson or exercise to a menu sidebar view that displays the categories, so that the category of the lesson or exercise is highlighted. For this, I need to understand how to do such a task in laravel.</p>
  
  <p>From what I gathered, this is often done via controllers. However, there is no menu controller, but rather a menu composer. It contains a function</p>
  
  <pre><code>class MenuComposer
  {
      public function compose(View $view)
      {
          $minutes = 6 * 60;
          $value = Cache::remember('menu-categories', $minutes, function()       {
          return \App\Category::with('parent')-&gt;with('children')-&gt;get();
          });
  
          $view-&gt;with('categories', $value);
      }
  }
  </code></pre>
  
  <p>Then in the menu blade file we have</p>
  
  <pre><code>@foreach ($categories as $category)
  &lt;?php $category = $category-&gt;present(); ?&gt;
  
  @if ($category-&gt;parent == null)
      &lt;li&gt;&lt;a href="{{ $category-&gt;url }}"&gt;{{ $category-&gt;title }}&lt;/a&gt;&lt;/li&gt;
  
      @foreach ($category-&gt;children as $child)
          &lt;?php $child = $child-&gt;present() ?&gt;
          &lt;li class="level1"&gt;&lt;a href="{{ $child-&gt;url }}"&gt;{{ $child-&gt;title }}&lt;/a&gt;&lt;/li&gt;
  
          &lt;?php
              /*
              @foreach ($child-&gt;children as $grandChild)
                  &lt;?php $grandChild = $grandChild-&gt;present() ?&gt;
                  &lt;li class="level2"&gt;&lt;a href="{{ $grandChild-&gt;url }}"&gt;{{ $grandChild-&gt;title }}&lt;/a&gt;&lt;/li&gt;
              @endforeach
              */
          ?&gt;
  
          @endforeach
  
      @endif
  
  @endforeach
  </code></pre>
  
  <p>So this is clear. I see that I can use the menu composer to pass additional data with a $view->with() call. </p>
  
  <p>The question is how do I get the current category? For exercises and lessons, the routes don't have category data. They are of form</p>
  
  <pre><code>lesson/lessonid/lessontitle
  </code></pre>
  
  <p>or </p>
  
  <pre><code>exercise/exid/extitle
  </code></pre>
  
  <p>So I know I could do some sort of query of the model. But seems that wouldn't make sense, since I know there are other places in the process flow where the current cat is being passed. For instance, on an exercise page, the view is retrieving category as</p>
  
  <pre><code>$exercise-&gt;lesson-&gt;category-&gt;title
  </code></pre>
  
  <p>It is being passed this in exercise controller as</p>
  
  <pre><code>    public function index($id, $name = null)
  {
      //$this-&gt;hit($id);
      $exercise = $this-&gt;apiController-&gt;get($id);
      $authorized = $this-&gt;isUserAuthorized();
  
      return view('exercise/index', [
              'exercise' =&gt; $exercise-&gt;present(),
              'authorized' =&gt; $authorized,
      ]);
  }
  </code></pre>
  
  <p>Similarly, a lesson controller passes $lesson object to lesson view as</p>
  
  <pre><code>    public function index($id, $name = null)
  {
      //$this-&gt;hit($id);
      $lesson = $this-&gt;apiController-&gt;get($id);
      $subscribed = $this-&gt;request-&gt;user() &amp;&amp; $this-&gt;request-&gt;user()-&gt;subscribed('premium');
  
      return view('lesson/index', [
          'lesson' =&gt; $lesson-&gt;present(),
          'subscribed' =&gt; $subscribed,
      ]);
  }
  </code></pre>
  
  <p>Based on above, seems I could modify the return statements in the lesson and exercise controller to pass the category to the menu view, but I don't see in the documentation how to do that, and I suspect the menu view is rendered before the lesson and exercise controller are called...</p>
  
  <p>Also read about using service providers. middleware, etc, here: <a href="https://stackoverflow.com/questions/28608527/how-to-pass-data-to-all-views-in-laravel-5">How to pass data to all views in Laravel 5?</a></p>
  
  <p>But all these approaches seem overkill. I don't need every view to have the data. Seems to me, I need to do this somehow in the menu composer. But I don't know what method to use from the menu composer to retrieve the current lesson or exercise category. In the menu composer after debugging in phpstorm I see that the $view object for a lesson has $view->$data->$lesson->$entity.</p>
  
  <p>So what I did was edited the menu composer to pass category to view:</p>
  
  <pre><code>    $d=$view-&gt;getdata();
      $s=array_key_exists ('lesson' , $d );
      if ($s ==1) $attr = collect($d)-&gt;get('lesson');
      $cat=$attr-&gt;cat();
  </code></pre>
  
  <p>This works since in the LessonPresenter I added function</p>
  
  <pre><code>    public function cat()
  {
      $cat = $this-&gt;entity-&gt;category['attributes']['title'];
      return $cat;
  }
  </code></pre>
  
  <p>This works, but I feel like it is a hack. And I will have to do this for the Exercise Presenter as well. Being new to larval I suspect there has to be a more elegant way to do this. So can someone please explain how this should be done?</p>
  
  <p>thanks,</p>
  
  <p>Brian</p>