title: Laravel Code not DRY
body: <p>I'm working on an mlm site, And this is the code I have to get referrals of a referral down to the third generation. As for the requirements I must not use eloquent relationships. Here's my code;</p>
  
  <pre><code>public function matrix()
  {      
      $referrals = DB::table('users')-&gt;where('referred_by', Auth::user()-&gt;referral_id)-&gt;get();
  
  
      if (!isset($referrals))
      {
          return view('matrix');
      }
      elseif(isset($referrals))
      {
          $referrals2gen = DB::table('users')-&gt;where('referred_by', $referrals[0]-&gt;referral_id)-&gt;get();
          if (!isset($referrals2gen)) 
          {
              return view('matrix2', ['referrals' =&gt; $referrals]);
          }
          elseif(isset($referrals2gen))
          {
              $referrals3gen = DB::table('users')-&gt;where('referred_by', $referrals2gen[0]-&gt;referral_id)-&gt;get();
              if (!isset($referrals3gen[0]))
              {
                  return view('matrix3', ['referrals' =&gt; $referrals, 'referrals2gen' =&gt; $referrals2gen]);
              }
              elseif(isset($referrals3gen[0]))
              {
                  return view('matrix4', ['referrals' =&gt; $referrals, 'referrals2gen' =&gt; $referrals2gen, 'referrals3gen' =&gt; $referrals3gen]);
              }
  
          }
      }
  }  
  </code></pre>
  
  <p>Firstly, the code does not follow the Don't Repeat Yourself Principle. Secondly, it does not give all the referrals of my direct referrals. Any help I can get would be appreciated. </p>