title: laravel load candidates with given skills
body: <p>In my website users can create candidates (one-many relation) and those candidates can have skills (many-many).</p>
  
  <p>User model:</p>
  
  <pre><code>public function candidates() {
      return $this-&gt;hasMany(Candidate::class);
  }
  </code></pre>
  
  <p>candidate model:</p>
  
  <pre><code> public function skills() {
      return $this-&gt;belongsToMany(Skill::class, 'candidate_skill', 'candidate_id', 'skill_id');
  }
  </code></pre>
  
  <p>Skills model:</p>
  
  <pre><code>public function candidates() {
      return $this-&gt;belongsToMany(Candidate::class, 'candidate_skill', 'candidate_id', 'skill_id')
          -&gt;withTimestamps();
  }
  </code></pre>
  
  <p>I already have an index page where an user can view ALL his made candidates</p>
  
  <pre><code>$candidates = Auth::user()-&gt;candidates;
  </code></pre>
  
  <p>on the edit page, skills can get synced to the candidate in question</p>
  
  <pre><code>  $candidate-&gt;skills()-&gt;sync(request()-&gt;skills);
  </code></pre>
  
  <p>and back to the index page it shows how many skills the candidates have</p>
  
  <pre><code>&lt;td&gt;{{count($candidate-&gt;skills)}}&lt;/td&gt;
  </code></pre>
  
  <p>now, I need to make a search bar. My table already has one(dataTable) to search in the already loaded td's. But I need a search bar to search for candidates which have certain skills, say I want to search for candidates which are synced with 'css' and only show those in the table. </p>
  
  <p>I read through the laravel docs and tried eager loading it:</p>
  
  <pre><code>$hasCss = $candidates-&gt;load(['skills' =&gt; function ($query) {
          $query-&gt;where('name', '=',  'css');
      }]);
  </code></pre>
  
  <p>but this just loaded all candidates with only the css skill displayed, even the candidates who dont have it. I want to only load candidates who have the skill and leave the others out.</p>
  
  <p>How do I do this, I'm clueless :s</p>