title: Laravel - Passing data from multiple methods from the same controller to a partial view
body: <p>I have a simple phone book application with a main view, <strong>index.blade.php</strong> that includes a partial view, <strong>contact_list.blade.php</strong>.</p>
  <p>I only have a single route that refers to my controller, <strong>ContactListController.php</strong>.</p>
  <p>From inside <strong>contact_list.blade.php</strong> I need access to three methods of my controller.</p>
  <p>One of the methods returns the data I need to pass as parameter for the other two.</p>
  <p>Here's what my application looks like so far:</p>
  <p><a href="https://i.stack.imgur.com/rNFvW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rNFvW.png" alt="enter image description here" /></a></p>
  <p>I want to be able to type a name in the search form and then filter the names of usuarios in the <strong>contact_list</strong> view.</p>
  <p>Here's <strong>search_contact.blade.php</strong>, where the search form is located:</p>
  <pre><code>&lt;div class=&quot;col-lg-4 inp&quot;&gt;
      &lt;form action=&quot;.&quot; method=&quot;GET&quot;&gt;
          &lt;div class=&quot;form-group&quot;&gt;
              &lt;input type=&quot;text&quot; name=&quot;searchName&quot; id=&quot;searchName&quot; class=&quot;form-control mt-2 mb-3&quot; placeholder=&quot;search&quot;&gt;
              &lt;input class=&quot;btn btn-info w-100 btn1&quot; type=&quot;submit&quot; value=&quot;search&quot;&gt;
          &lt;/div&gt;
      &lt;/form&gt;
  &lt;/div&gt;
  </code></pre>
  <p>Here's <strong>contact_list.blade.php</strong>:</p>
  <pre><code>&lt;div class=&quot;col-lg-8&quot;&gt;
  
      &lt;table id=&quot;myTable&quot; class=&quot;table text-justify table-striped&quot;&gt;
          &lt;thead class=&quot;tableh1&quot;&gt;
              &lt;th class=&quot;&quot;&gt;Name&lt;/th&gt;
              &lt;th class=&quot;col-3&quot;&gt;Nº of Phones&lt;/th&gt;
              &lt;th class=&quot;&quot;&gt;Phones&lt;/th&gt;
          &lt;/thead&gt;
  
          &lt;tbody id=&quot;tableBody&quot;&gt;
  
              @if (!isset($_GET[&quot;searchName&quot;]))
              @foreach ($data as $item)
              &lt;tr&gt;
                  &lt;!-- Test --&gt;
                  &lt;td&gt;{{ $item-&gt;nome }}&lt;/td&gt;
                  &lt;td&gt;{{ $item-&gt;numero_count }}&lt;/td&gt;
                  &lt;td&gt;
                      &lt;ul style=&quot;list-style: none;&quot;&gt;
                          &lt;?php $phones = app\Http\Controllers\ContactListController::get_telefones_by_usuario($item-&gt;u_id) ?&gt;
                          @foreach ($phones as $phone)
                          &lt;li&gt;&lt;i class=&quot;bi bi-telephone-fill pe-2&quot; style=&quot;color: #0d6efd&quot;&gt;&lt;/i&gt;{{ $phone-&gt;numero }}&lt;/li&gt;
                          @endforeach
                      &lt;/ul&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
              @endforeach
              @else
              &lt;?php $my_data = app\Http\Controllers\ContactListController::get_users_by_name(request()) ?&gt;
  
              @foreach ($my_data as $item)
              &lt;tr&gt;
                  &lt;!-- Test --&gt;
                  &lt;td&gt;{{ $item-&gt;nome }}&lt;/td&gt;
                  &lt;td&gt;{{ $item-&gt;numero_count }}&lt;/td&gt;
                  &lt;td&gt;
                      &lt;ul style=&quot;list-style: none;&quot;&gt;
                          &lt;?php $phones = app\Http\Controllers\ContactListController::get_telefones_by_usuario($item-&gt;u_id) ?&gt;
                          @foreach ($phones as $phone)
                          &lt;li&gt;&lt;i class=&quot;bi bi-telephone-fill pe-2&quot; style=&quot;color: #0d6efd&quot;&gt;&lt;/i&gt;{{ $phone-&gt;numero }}&lt;/li&gt;
                          @endforeach
                      &lt;/ul&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
              @endforeach
  
              @endif
          &lt;/tbody&gt;
      &lt;/table&gt;
  &lt;/div&gt;
  </code></pre>
  <p>Here are my three methods inside my controller:</p>
  <pre><code>    public function index()
      {
          // $usuarios = Usuario::all();
          // $telefones = Telefone::all();
          $data = Usuario::join(&quot;telefones&quot;, &quot;usuarios.id&quot;, &quot;=&quot;, &quot;telefones.id_usuario&quot;)
              -&gt;select(
                  &quot;usuarios.id as u_id&quot;,
                  &quot;usuarios.nome as nome&quot;,
                  &quot;telefones.numero as numero&quot;,
                  DB::raw(&quot;COUNT(telefones.numero) AS numero_count&quot;)
              )
              -&gt;groupBy(&quot;usuarios.nome&quot;)
              -&gt;orderBy(&quot;usuarios.nome&quot;)
              -&gt;get();
  
          return view(&quot;index&quot;, compact(&quot;data&quot;));
      }
  </code></pre>
  <pre><code>    public static function get_telefones_by_usuario($id)
      {
  
          $telefones = Telefone::join(&quot;usuarios&quot;, &quot;usuarios.id&quot;, &quot;=&quot;, &quot;telefones.id_usuario&quot;)
              -&gt;select(&quot;telefones.numero as numero&quot;)
              -&gt;where(&quot;usuarios.id&quot;, &quot;=&quot;, $id)
              -&gt;get();
  
          return $telefones;
          // return view(&quot;index&quot;, compact(&quot;telefones&quot;));
      }
  </code></pre>
  <pre><code>    public static function get_users_by_name(Request $request)
      {
          $name = $request-&gt;input(&quot;searchName&quot;);
  
          $usuarios = Usuario::join(&quot;telefones&quot;, &quot;usuarios.id&quot;, &quot;=&quot;, &quot;telefones.id_usuario&quot;)
              -&gt;select(
                  &quot;usuarios.id as u_id&quot;,
                  &quot;usuarios.nome as nome&quot;,
                  &quot;telefones.numero as numero&quot;,
                  DB::raw(&quot;COUNT(telefones.numero) AS numero_count&quot;)
              )
              -&gt;where(&quot;usuarios.nome&quot;, &quot;LIKE&quot;, &quot;$&quot; . $name . &quot;%&quot;)
              -&gt;groupBy(&quot;usuarios.nome&quot;)
              -&gt;orderBy(&quot;usuarios.nome&quot;)
              -&gt;get();
  
          return $usuarios;
      }
  </code></pre>
  <p>I call my controller method from inside the partial view, but I heard it's bad practice to do that. If that's the case, then how can I achieve what I want?</p>
  <p>My idea was to access the GET parameter <code>searchName</code> (from the form inside <strong>search_contact.blade.php</strong> partial view) from within my <strong>contact_list</strong> view, by calling the controller method. But I'm getting zero results back with my code as it is:</p>
  <p><a href="https://i.stack.imgur.com/eDqyg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eDqyg.png" alt="enter image description here" /></a></p>
  <p>How can I achieve what I want? What am I doing wrong?</p>
  <p>Is it really that bad to call a controller method from inside a view? If so, what can I do instead?</p>
  <p>Thank you.</p>