title: Why there is error Builder::query does not exist in laravel?
body: <p>In index function I have query, based on roles and id the query getting change. </p>
  
  <p>Following is script in my controller in index method </p>
  
  <pre><code>public function index(Request $request)
  {
  
      $data = $request-&gt;validate([
          'id' =&gt; 'sometimes|nullable|string|max:10',
      ]);
  
  
      $networks = [];
  
      $user   = Auth::user();
  
      if($user-&gt;hasRole('admin')){
          $net = Network::query();
      }
  
      else if($user-&gt;hasRole('distributor')){
          $net = Network::whereHas('users', function($q) use ($user){
              $q-&gt;whereHas('roles', function($r){
                  $r-&gt;whereIn('name', ['admin', 'owner']);
              })-&gt;where('parent_id', $user-&gt;id);
          })-&gt;query();
      }
  
      else if($user-&gt;hasRole('agent') || $user-&gt;hasRole('owner'))
          $net = Network::whereHas('users', function($q) use ($user){
              $q-&gt;where('email', $user-&gt;email);
          })-&gt;query();
  
      $ids_ordered = null;
  
      if(isset($data['id']) &amp;&amp; $data['id']){
          $ids_ordered = $data['id'];
          $net = $net-&gt;orderByRaw(DB::raw("FIELD(id, $ids_ordered) desc"))
              -&gt;paginate(3);
  
      }
  
      if($user-&gt;hasRole('admin') || $user-&gt;hasRole('distributor') || $user-&gt;hasRole('owner'))
          $ownerView = true;
  
      else $ownerView = false;
  
      $net = $net-&gt;get();
      if(!empty($net))
  
      $networks = $net-&gt;mapWithKeys(function($network) use ($ownerView, $ids_ordered){
  
          $coinSold   = $this-&gt;userNetworkRepo-&gt;getcoinSold($network-&gt;owner()-&gt;id);
          $allocated  = $this-&gt;userNetworkRepo-&gt;getAllocatedNez($network-&gt;owner()-&gt;id);
          $open       = false;
  
          if($ids_ordered){
              $ids_ordered = explode(',', $ids_ordered);
  
              if(in_array($network-&gt;id, $ids_ordered))
                  $open = true;
          }
  
          $hierarchy = [
              'owner'             =&gt; $network-&gt;owner(),
              'agents'            =&gt; $network-&gt;agents(),
              'drawAgents'        =&gt; $this-&gt;drawAgentTree($network-&gt;id, true),
              'ownercoinSold'     =&gt; $coinSold,
              'ownerAllocated'    =&gt; $allocated,
              'open'              =&gt; $open,
          ];
  
          if ($distributor = $network-&gt;distributor()) {
             $hierarchy['distributor'] = $distributor;
          }
  
          $hierarchy['coinSold'] = [
              'ownerView'     =&gt; $ownerView
          ];
  
          return [$network-&gt;name =&gt; $hierarchy];
  
          //return [$network-&gt;distributor() ? $network-&gt;distributor()-&gt;getFullName() : 'no-partner' =&gt; $hierarchy];
  
      });
  
      if($request-&gt;ajax()){
          $view = view('admin.networks.partials.displayNetworkIndex', compact('networks'))-&gt;render();
          return response()-&gt;json(['html' =&gt; utf8_encode($view)],  200, ['Content-type'=&gt; 'application/json; charset=utf-8'], JSON_UNESCAPED_UNICODE);
      }
  
      return view('admin.networks.index', compact('networks'));
  }
  </code></pre>
  
  <p>But when I access page I am getting following error </p>
  
  <blockquote>
    <p>Method Illuminate\Database\Query\Builder::query does not exist.</p>
  </blockquote>
  
  <p>why this error showing? Where is something wrong with this query?</p>
  
  <p>I have searched about it on google and found some threads but those about <code>keyBy</code>.</p>