title: Laravel-Many-to-one Polymorphic relationship
body: <p>I am using laravel 5.1. The scenario is as follows(this is an example. The real scenario is similar to this example)</p>
  
  <p>I have 3 models</p>
  
  <ol>
  <li>College  </li>
  <li>Student  </li>
  <li>Teacher</li>
  </ol>
  
  <p>A college can have many students but a student can belong to only 1 college. </p>
  
  <p>A college can have many teachers but a teacher can belong to only 1 college.</p>
  
  <p>I want to establish relationships between these tables in laravel.
  One of the methods for this is to place a <em>college_id</em> foreign key on the Students and Teachers table. But in my case, this foreign key will be null a lot of times. So rather than have separate columns in 3-4 tables with mostly null values, I wanted to explore the option of having a polymorphic relationship for College table.</p>
  
  <p><strong>This is what I tried:</strong>
  The example given in the laravel docs(link below) depict a one-to-many relationship whereas my scenario is more of a many to one relationship.</p>
  
  <p><a href="http://laravel.com/docs/5.1/eloquent-relationships#polymorphic-relations" rel="nofollow">http://laravel.com/docs/5.1/eloquent-relationships#polymorphic-relations</a></p>
  
  <p>As given in the example, having collegeable_id and collegeable_type columns on the College table would not have fulfilled my requirement as a college can contain many students/teachers so I created a pivot table:</p>
  
  <pre><code>Schema::create('collegeables', function (Blueprint $table) {
          $table-&gt;integer('college_id')-&gt;unsigned();
          $table-&gt;integer('collegeable_id')-&gt;unsigned();
          $table-&gt;string('collegeable_type');
      });
  </code></pre>
  
  <p>And I have the following models</p>
  
  <p>College Model:</p>
  
  <pre><code>    namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class College extends Model
  {
      public function students()
      {
          return $this-&gt;morphedByMany('App\Student', 'collegeable');
      }
  }
  </code></pre>
  
  <p>Student Model:</p>
  
  <pre><code>    namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Student extends Model
  {
      public function college()
      {
          return $this-&gt;morphOne('App\Colleges', 'collegeable');
      }
  }
  </code></pre>
  
  <p>With this arrangement, I am able to store students using College model instance like this</p>
  
  <pre><code>$college = \App\College::find(1);
  $student = new \App\Student;
  $student-&gt;name = 'John Doe';
  $college-&gt;students()-&gt;save($student);
  </code></pre>
  
  <p>But when I try to retrieve a College model instance using a student model instance as specified below, it gives me an error:-</p>
  
  <pre><code>public function index()
      {
          return \App\Student::find(1)-&gt;college;
      }
  </code></pre>
  
  <p>SQLSTATE[42S22]: Column not found: 1054 Unknown column 'colleges.collegeable_id'</p>
  
  <p>This is kind of expected as morphOne works with columns in a table I suppose.
  If I change the morphOne function in Student Model to morphToMany, the code starts working and I am able to retrieve values as well. But that make this relationship a many to many which again is not what I want.</p>
  
  <p><strong>So my question is this:-
  Is their a morphSomething function I can use in the student model to be able to retrieve values for the student's college while maintaining the relationship as a one-to-many?</strong></p>
  
  <p>Any help would be really appreciated. Thanks.</p>