2015-12-12 45 views
0

我似乎有理解這種等級關係的問題。laravel雄辯的關係層次

農場>字段>牧羊犬>綿羊

這似乎是一個非常簡單的層次 - 農場的hasMany場,現場的hasMany牧羊人,牧羊人的hasMany羊。

羊屬於牧羊人,牧羊人屬於田地,田地屬於農場。

我已經這樣定義這個模型的關係:

class Sheep extends Model { 

    protected $fillable ['name']; 

    public function shepherd() { 
      return $this->belongsTo('App\Shepherd'); 
     }  
} 


class Shepherd extends Model { 

    protected $fillable ['name']; 

    public function field() { 
      return $this->belongsTo('App\Field'); 
     }  
    public function sheep() { 
      return $this->hasMany('App\Sheep'); 
    }    
} 

class Field extends Model { 

    protected $fillable ['name']; 

    public function farm() { 
      return $this->belongsTo('App\Farm'); 
     }  

    public function shepherd() { 
      return $this->hasMany('App\Shepperd'); 
    } 
} 

class Farm extends Model { 

    protected $fillable ['name']; 

    public function field() { 
      return $this->hasMany('App\Field'); 
     }  
} 

public function up() 
{ 
    Schema::create('farms', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->string('name'); 
    }); 
} 

public function up() 
{ 
    Schema::create('fields', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->integer('farm_id'); 
     $table->string('name'); 
    }); 
} 
public function up() 
    Schema::create('shepherds', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->integer('field_id'); 
     $table->string('name'); 
    }); 
} 
public function up() 
    Schema::create('sheep', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->integer('shepherd_id'); 
     $table->string('name'); 
    }); 
} 

我希望能夠爲每個模型保存在以下方式。

$farm = new App\Farm; 

$farm->name = 'West Farm'; 

$field = new App\Field; 

$field->name = 'The orchard'; 

$shepherd = new App\Shepherd; 

$shepherd->name = 'Jason'; 

$sheep = new App\Sheep; 

$sheep->name = 'Sean'; 

$farm->save(); 

$farm->field()->save($farm); 

$farm->field->shepherd()->save($shepherd); 

$farm->field->shepherd->sheep()->save($sheep); 

但它不起作用。一旦我到達$farm->field->shepherd()->save($shepherd);,進程就會崩潰。我希望能夠以正確的方式保存所有表格之間的關係。

我拉我的頭髮試圖瞭解這一點,所以任何幫助將不勝感激。

感謝

+0

你能否請添加一些信息,如錯誤,預期輸出/實際輸出等? –

+0

我得到的錯誤如下 - 調用undefined方法Illuminate \ Database \ Eloquent \ Collection :: shepherd()。這發生在我嘗試$ farm-> field-> shepherd() - >保存($ shepherd)時。但是,如果我嘗試$ field-> shepherd() - > save($ shepherd)那麼這是行得通的,爲什麼我不能做$ farm-> field-> shepherd() - > save($ shepherd)? – Basicmanthz

回答

0

您的代碼打破這裏:

$farm->field->shepherd()->save($shepherd); 

農場有許多領域,所以當你引用$場 - >字段,你得到對象的集合,不只是一個字段對象。

要使其工作,你需要或者參考$場 - >字段[0]

$farm->field[0]->shepherd()->save($shepherd); 

或只使用$場對象創建之前:

$field->shepherd()->save($shepherd); 

我也建議您使用複數名稱作爲您的hasMany關係(字段,羊等) - 這種方式您將永遠記住所引用的字段指的是一個公司選舉,而不是一個單一的對象

+0

好吧,我想我正在讓我的邏輯倒退。我可以說$ sheep-> shepherd-> field-> farm,因爲每個參考都是針對單個項目。那麼是否可以定義我的所有對象$ sheep,$ shepherd,$ field,$ farm並將它們保存爲一個單一的函數,以便創建所有關係? – Basicmanthz

+0

是的。一旦你正確地設置了關係,你可以調用push()而不是save() - 它將在當前模型上調用save()並在所有關係上調用push(),然後調用所有相關模型上的save()和push()在他們所有的關係等... –

+0

這是真棒,感謝您的解釋 – Basicmanthz