title: laravel eloquent relationship hierarchy
body: <p>I seem to be having a problem understanding this hierarchical relationship.</p>
  
  <p>Farm > Fields > Shepherds > Sheep</p>
  
  <p>It seems a pretty straightforward hierarchy - Farm hasMany fields, field hasMany shepherds, shepherd hasMany sheep.</p>
  
  <p>Sheep belong to a shepherd, shepherds belongs to fields, fields belong to farms.</p>
  
  <p>I have defined this model relationships thus:</p>
  
  <pre><code>class Sheep extends Model {
  
      protected $fillable ['name'];
  
      public function shepherd() {
              return $this-&gt;belongsTo('App\Shepherd');
          }    
  }
  
  
  class Shepherd extends Model {
  
      protected $fillable ['name'];
  
      public function field() {
              return $this-&gt;belongsTo('App\Field');
          }    
      public function sheep() {
              return $this-&gt;hasMany('App\Sheep');
      }               
  }
  
  class Field extends Model {
  
      protected $fillable ['name'];
  
      public function farm() {
              return $this-&gt;belongsTo('App\Farm');
          }    
  
      public function shepherd() {
              return $this-&gt;hasMany('App\Shepperd');
      }   
  }
  
  class Farm extends Model {
  
      protected $fillable ['name'];
  
      public function field() {
              return $this-&gt;hasMany('App\Field');
          }    
  }
  
  public function up()
  {
      Schema::create('farms', function (Blueprint $table) {
          $table-&gt;increments('id');
          $table-&gt;string('name');
      });
  }
  
  public function up()
  {
      Schema::create('fields', function (Blueprint $table) {
          $table-&gt;increments('id');
          $table-&gt;integer('farm_id');
          $table-&gt;string('name');
      });
  }
  public function up()
      Schema::create('shepherds', function (Blueprint $table) {
          $table-&gt;increments('id');
          $table-&gt;integer('field_id');
          $table-&gt;string('name');
      });
  }
  public function up()
      Schema::create('sheep', function (Blueprint $table) {
          $table-&gt;increments('id');
          $table-&gt;integer('shepherd_id');
          $table-&gt;string('name');
      });
  }
  </code></pre>
  
  <p>I would expect to be able to save each model in the following manner.</p>
  
  <pre><code>$farm = new App\Farm;
  
  $farm-&gt;name = 'West Farm';
  
  $field = new App\Field;
  
  $field-&gt;name = 'The orchard';
  
  $shepherd = new App\Shepherd;
  
  $shepherd-&gt;name = 'Jason';
  
  $sheep = new App\Sheep;
  
  $sheep-&gt;name = 'Sean';
  
  $farm-&gt;save();
  
  $farm-&gt;field()-&gt;save($farm);
  
  $farm-&gt;field-&gt;shepherd()-&gt;save($shepherd);
  
  $farm-&gt;field-&gt;shepherd-&gt;sheep()-&gt;save($sheep);
  </code></pre>
  
  <p>But it does not work. Once I get to <code>$farm-&gt;field-&gt;shepherd()-&gt;save($shepherd);</code> the process breaks down. I would appreciate some assistance in the correct manner of saving using the relationships between all the tables.</p>
  
  <p>I'm pulling my hair out trying to understand this, so any help would be greatly appreciated.</p>
  
  <p>thanks</p>