title: Correct way to run a select query from blades in laravel 5 using eloquent
body: <p>What is the Correct Way to retrieve a column value based on certain select filter on a Model variable availed by <code>compact</code> method inside the blade. (Larevl 5)<br>
  I read that Its a bad practice to query database staright from views, and hence i followed the convention to avail the required data with <code>compact</code> method to view</p>
  
  <p>However, In a scenario where I need to query another table based on certain column value returned in foreach loop inside a blade from first table, I am unable to figure out correct Approach</p>
  
  <p>Example: I have two Models <code>User</code> &amp; <code>Group</code>
  <br>Schema  <code>User</code> Table</p>
  
  <pre><code>id,name,email,group_id
  </code></pre>
  
  <p>Scheme Group Table<br>
  <code>id</code>,<code>groupname</code></p>
  
  <p>Here is the UserController -> compact method</p>
  
  <pre><code>$users = \App\User::all(array('id','name','email','group_id'));
  $groups = \App\Group::all(array('id','group_name'));
  return view('user.index',  compact('users','groups'));
  </code></pre>
  
  <p>Here how the blade needs them </p>
  
  <pre><code>@foreach ($users as $user)
        &lt;tr&gt;
              &lt;th&gt;{{$user-&gt;id}}&lt;/th&gt;
              &lt;th&gt;{{$user-&gt;name}}&lt;/th&gt;
              &lt;th&gt;{{$user-&gt;email}}&lt;/th&gt;
              &lt;th&gt;
                &lt;!-- Here i need to run something like 
  select group_name from group where id = $user-&gt;id --&gt;
  {{$groups-&gt;where('id','=',$user-&gt;group_id) }}
  
              &lt;/th&gt;
              &lt;th&gt;Actions&lt;/th&gt;
          &lt;/tr&gt;
          @endforeach
  </code></pre>
  
  <p>I know this returns an array , and I'have two questions here</p>
  
  <ol>
  <li>How to get the value for <code>group_name</code> column from the Group Model based on group.<code>id</code> = <code>$user-&gt;id</code> in a foreach loop </li>
  <li>Since Its a bad practice to query db from blade, how would I avail the values from a model by passing data via compact from controller to blade, when the where clause parameter's are not yet known.</li>
  </ol>
  
  <p><strong>Edit 1:</strong></p>
  
  <p>I modified the last group query as </p>
  
  <pre><code> &lt;th&gt;@if($groups-&gt;where('id','=',$user-&gt;group_id))
                    @foreach($groups as $group)
        {{$group-&gt;group_name}}            
                    @endforeach
                  @endif
  &lt;/th&gt;
  </code></pre>
  
  <p>And I was able to get the result, however this again isn't a correct approach , so question remain unanswered</p>