2015-01-29 5 views
0

를 부착 ... 그것은 조금 복잡하고 혼란 ... 하지만 난 모델이라는 건물이 있고이 같은 모델의 관계를 가지고 있지만, belongsToMany 관계와laravel belongsToMany 자체 참조 관계

이 모델에서 나는 다음과 같은 한 관계 :

# Building Require one or more another Building(s) 
    public function requires() { 
     return $this->belongsToMany('Building', 'building_require')->withPivot(array(
      'level', 
      'updated_at', 
      'created_at' 
     )); 
    } 

그리고 지금은 내 중간 테이블 building_require을 입력합니다 :

$obj = Building::find(1); 
$obj->requires()->attach(4, array(
     'level' => 1, 
     'created_at' => new DateTime, 
     'updated_at' => new DateTime, 
)); 

또한, p

+-----+--------------+-------------+--------+----------------------+---------------------+ 
| id | building_id | require_id | level |  created_at  |  updated_at  | 
+-----+--------------+-------------+--------+----------------------+---------------------+ 
| 1 |   1 |   4 |  1 | 2015-01-29 13:33:45 | 2015-01-29 13:33:45 | 
+-----+--------------+-------------+--------+----------------------+---------------------+ 

왜 이런 일이 .. 나는이 기대

+-----+--------------+-------------+--------+----------------------+---------------------+ 
| id | building_id | require_id | level |  created_at  |  updated_at  | 
+-----+--------------+-------------+--------+----------------------+---------------------+ 
| 1 |   4 |   0 |  1 | 2015-01-29 13:33:45 | 2015-01-29 13:33:45 | 
+-----+--------------+-------------+--------+----------------------+---------------------+ 

그러나 이것은 잘못된 것입니다 : roblem : 내 테이블에서 나는 지금이 행을 찾을?

# Building Require one or more another Building(s) 
    public function requires() { 
     return $this->belongsToMany('Building', 'building_require', 'building_id', 'require_id')->withPivot(array(
      'level', 
      'updated_at', 
      'created_at' 
     )); 
    } 

아마도 누군가가 도움이 될 것입니다 :

답변

0

나 자신을 대답 할 수는 .. 관계가 있어야한다.

관련 문제