title: Laravel Eloquent pass variable to with relationship function
body: <p>I am using eloquent to make a query for a database entry and it's corresponding relationship. The problem is I need to pass the <code>$date</code> variable into the relationship <code>query</code> like shown below: </p>
  
  <p>I can pass the <code>$date</code> variable to the first query because its not inside a with function. How can I achieve this with the second?</p>
  
  <p><strong>Query</strong></p>
  
  <pre><code>    public static function find_task($type, $date) {
      if($type == 'student') {
          $mySid = Student::student_id();
          $allTasks = TaskAssignment::where('student_id', $mySid)
              -&gt;with('task')
              -&gt;where('dueDate', $date)
              -&gt;orderBy('dueDate', 'asc')
              -&gt;get();
  
      } elseif($type == 'teacher') {
          $myTid = Teacher::teacher_id();
          $allTasks = Tasks::where('teacher_id', $myTid)
              -&gt;with(['assignment' =&gt; function ($query) {
                  $query-&gt;where('dueDate', $date);
                  $query-&gt;orderBy('dueDate', 'asc');
  
              }])-&gt;get();
      } else {
          return 'error this page does not exist';
      }
  
      return $allTasks;
  }
  </code></pre>