title: Best way to query model relationship in Eloquent
body: <p>I am working within a controller in a Laravel application.  I am returning a table to the view.  The table is based on my PlanSubmission model.  I am receiving parameters through a GET request and using those parameters to return a filtered set of rows to my view.</p>
  <p>The first part of my controller looks like this and is working fine:</p>
  <pre class="lang-php prettyprint-override"><code>public function index()
      {
  
      //Used for filter.  The request is received in the URL
      if (request()-&gt;has('status')) {
          $plans = PlanSubmission::where('status', request('status'))-&gt;paginate(25)-&gt;appends('status', request('status'));
      }
      elseif (request('employer_name')) {
          $plans = PlanSubmission::where('employer_name', request('employer_name'))-&gt;paginate(25)-&gt;appends('employer_name', request('employer_name'));
          }
  </code></pre>
  <p>I have run into a problem because now I need to use a model relationship in the controller.  I am receiving 'advisor_name' from the request.  The 'advisor_id&quot; column is the foreign key on the PlanSubmission model.  The 'advisor_name' column exists in the Advisor model.  I have a function on my PlanSubmission model that looks like this:</p>
  <pre><code>    public function advisor()
  {
      return $this-&gt;belongsTo(Advisor::class);
  }
  </code></pre>
  <p>Initially, I thought there was a way I could do this easily with something like:</p>
  <pre><code>            $plans = PlanSubmission::where(advisor-&gt;name, request('advisor_name'))-&gt;paginate(25)-&gt;appends('advisor_name', request('advisor_name'));
  </code></pre>
  <p>Of course, this will not work because I cannot enter a relationship into the first parameter in the Where Clause.</p>
  <p>I do not know where to go from here.  My other thought is to return all the advisors first from the Advisor model like this:</p>
  <pre><code>            $advisors = Advisor::where('name', request('advisor_name'));
  </code></pre>
  <p>Then, I imagine I would have to somehow loop through that and get the id (primary key) for each of the objects in $advisors and somehow get that into the PlanSubmission where clause.  I'm totally lost.</p>