title: Laravel inserting and retrieving relationships
body: <p>I'm working on a project built on Laravel 3, and I'm trying to see if I can shorten the code for working with relationships (better ways of doing the following)</p>
  
  <h2>user controller</h2>
  
  <p><em><strong>create user function</em></strong></p>
  
  <pre><code>$newUser = new User;
  
  if($userData['organization'])
      $newUser-&gt;organization = self::_professional('Organization', $newUser, $userData);
  else
      $newUser-&gt;school = self::_professional('School', $newUser ,$userData);
  </code></pre>
  
  <p><em><strong>create or retrieve school/organization id</em></strong></p>
  
  <pre><code>private function _professional($type, $newUser, $userData)
  {
      if ( $orgId = $type::where('name', '=', $userData[strtolower($type)])-&gt;only('id'))
          return $orgId;
      else
      {
          try {
              $org = $type::create(array('name' =&gt; $userData[strtolower($type)]));
              return $org-&gt;attributes['id'];
          } catch( Exception $e ) {
              dd($e);
          }
      }
  }
  </code></pre>
  
  <hr>
  
  <h2>Models</h2>
  
  <p><strong>User model</strong></p>
  
  <pre><code>class User extends Eloquent {
  
      public function organization()
      {
          return $this-&gt;belongs_to('Organization');
      }
  
      public function school()
      {
              return $this-&gt;belongs_to('School');
      }
  }
  </code></pre>
  
  <p><strong>Organization/School model</strong></p>
  
  <pre><code>class Organization extends Eloquent {
  
      public function user() 
      {
          return $this-&gt;has_many('User');
      }
  
  }
  </code></pre>
  
  <hr>
  
  <h2>Migrations</h2>
  
  <p><strong>Users migration</strong></p>
  
  <pre><code>....
  $table-&gt;integer('organization_id')-&gt;unsigned()-&gt;nullable();
  $table-&gt;foreign('organization_id')-&gt;references('id')-&gt;on('organizations');
  
  $table-&gt;integer('school_id')-&gt;unsigned()-&gt;nullable();
  $table-&gt;foreign('school_id')-&gt;references('id')-&gt;on('schools');
  ....
  </code></pre>
  
  <p><strong>Organizations/Schools migration</strong></p>
  
  <pre><code>....
  $table-&gt;increments('id');
  $table-&gt;string('name');
  $table-&gt;string('slug');
  $table-&gt;integer('count')-&gt;default(1)-&gt;unsigned();
  ....
  </code></pre>
  
  <p>Now, my questions are:</p>
  
  <ol>
  <li><p>Is there any better way of generating the User -> School/Organization relationship, then the one used above? If so, how?</p></li>
  <li><p>Any better way of retrieving the User's School/Organization name then by doing: <code>School::find($schoolId)-&gt;get()</code></p></li>
  </ol>
  
  <p>Doing <code>User::find(1)-&gt;school()</code> won't retrieve any data for <code>school</code>, only:</p>
  
  <pre><code>[base:protected] =&gt; User Object
  (
      [attributes] =&gt; Array
          (
              [id] =&gt; 1
              [nickname] =&gt; w0rldart
              ....
              [organization_id] =&gt; 
              [school_id] =&gt; 1
              ...
          )
      [relationships] =&gt; Array
          (
          )
  
      [exists] =&gt; 1
      [includes] =&gt; Array
          (
          )
  
  )
  
  [model] =&gt; School Object
  (
      [attributes] =&gt; Array
          (
          )
  
      [original] =&gt; Array
          (
          )
  
      [relationships] =&gt; Array
          (
          )
  
      [exists] =&gt; 
      [includes] =&gt; Array
          (
          )
  
  )
  </code></pre>