title: Laravel Increase SQL speed
body: <p>I am trying to increase the speed of my queries in <strong>Laravel 5.7</strong> and I have the call down to ~2.5 seconds. I am trying to figure out more ways to make it faster and if I could get some help I'd greatly appreciate it.</p>
  
  <p>Thanks</p>
  
  <p>How my data is structured: <a href="https://i.stack.imgur.com/IrZNp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IrZNp.png" alt="enter image description here"></a></p>
  
  <p>Function(Controller):</p>
  
  <pre><code>public function getUserDataTmp(Request $request) {
  
          $input = file_get_contents("php://input");
          $request = json_decode($input);
  
          if ($this-&gt;authTokenAccess($request) == true) {
  
              $bottomWords = bottom_exterior_word::select('word','sentence','sequence','id','group_id')-&gt;where('user_id','=', $request-&gt;id)-&gt;get();
  
              $emergencyWords = left_exterior_word::select('word','sentence','sequence','id')-&gt;where('user_id','=', $request-&gt;id)-&gt;get();
  
              foreach($bottomWords as $tmp =&gt; $key) {
                  $group_id = $key-&gt;group_id;
                  $bottomWords-&gt;user_id = $request-&gt;id;
                  $bottomWords[$tmp]-&gt;words = $key-&gt;getMainWords($group_id, $request-&gt;id);
              }
  
              foreach($emergencyWords as $key =&gt; $word) {
                  $emergencyWords[$key]-&gt;image = imageModel::select('base64','id')-&gt;where('emergency_id','=', $word-&gt;id)-&gt;first();
  
              }
  
              $data = [
                  'data' =&gt; [
                      'return' =&gt; 'success',
                      'code' =&gt; 'VEDC001',
                      'response' =&gt; 'Successfully Gathered Words',
                      'main_categories' =&gt; $bottomWords,
                      'emergency_words' =&gt; $emergencyWords
                  ]
              ];
  
              return(json_encode($data));
  
          }
  
      }
  </code></pre>
  
  <p>getMainWords Function(bottom_exterior_word model):</p>
  
  <pre><code>    public function getMainWords($group_id, $id)
    {
        // return("TEST");
        $words = \App\main_word::select('id','group_id','sentence','sequence','word')-&gt;where('group_id','=', $group_id)-&gt;where('user_id','=', $id)-&gt;get();
        foreach ($words as $key =&gt; $word) {
            $words[$key]-&gt;image = Image::select('base64','id')-&gt;where('word_id','=', $word-&gt;id)-&gt;first();
        }
        return $words;
    }
  </code></pre>