title: Laravel newbie and external (Moodle) database
body: <p>I'm trying to read from external Moodle database in Laravel Eloquent, to display all users that are teachers, and put them into a dropdown list inside a form.</p>
  
  <p>I have Teacher model containing:</p>
  
  <pre><code>class Teacher extends Eloquent
  {
      protected $guarded = ['id'];
      protected $connection = 'moodle';
      protected $table = 'mooddle_user';
  
      public function Task()
      {
          return $this-&gt;hasMany('Task', 'id');
      }
  }
  </code></pre>
  
  <p>I can return all users just simply by using lists method, but the problem is that the info whether the user is indeed a teacher or not is not stored in this table. Instead I have to use a few <code>JOIN</code> commands to retrieve teachers.</p>
  
  <p>I'm wondering how can I execute this command inside the model, to use it simply in the dropdown of the Task view. Task model, accepts teacher_id - foreign key.</p>
  
  <p>SQL:</p>
  
  <pre><code>SELECT DISTINCT u.firstname, u.lastname
  FROM moddle_user u
  JOIN moddle_user_enrolments ue ON ue.userid = u.id
  JOIN moddle_enrol e ON e.id = ue.enrolid
  JOIN moddle_role_assignments ra ON ra.userid = u.id
  JOIN moddle_role r ON r.id = ra.roleid AND r.shortname = 'editingteacher'
  WHERE e.status = 0 AND u.suspended = 0 AND u.deleted = 0
  AND (ue.timeend = 0 OR ue.timeend &gt; NOW()) AND ue.status = 0
  </code></pre>
  
  <p>I tried running it as Raw query through model but no luck, because it locks inside a <code>moddle_user</code> data table and so I am unable to use <code>JOIN</code> with other tables.</p>
  
  <p>I know I could've tried to create 4 additional eloquent models, but since I only need this one query to return all teachers, there could be another way around this.</p>
  
  <p>Any idea and suggestion greatly appreciated =).</p>