2013-06-11 2 views
0

'users'와 'nests'테이블과 'nest_user'피벗 테이블 2 개가 있습니다. 내 피벗 테이블에서 필자는 이메일 주소를 필터링하여 관련 이메일 addreses가있는 모든 중첩을 가져올 수 있습니다.Laravel 4 피벗 테이블 (many to many) 로딩되지 않음

public function up() 
{ 
    Schema::create('users', function(Blueprint $table) 
    { 
     $table->increments('id'); 
     $table->string('username'); 
     $table->text('bio'); 
     $table->string('picture'); 
     $table->string('email'); 
     $table->string('password'); 
     $table->integer('visits'); 
     $table->integer('nest_id'); 
     $table->timestamps(); 
    }); 
} 

    public function up() 
{ 
    Schema::create('nests', function(Blueprint $table) 
    { 
     $table->increments('id'); 
     $table->string('name'); 
     $table->string('info'); 
     $table->integer('note_id'); 
     $table->integer('website_id'); 
     $table->integer('image_id'); 
     $table->integer('video_id'); 
     $table->integer('location_id'); 
     $table->integer('user_id'); 
     $table->integer('share_id'); 
     $table->string('inviteAuth'); 
     $table->string('tid'); 
     $table->timestamps(); 
    }); 
} 

    public function up() 
{ 
    Schema::create('nest_user', function(Blueprint $table) 
    { 
     $table->increments('id'); 
     $table->integer('user_id'); 
     $table->integer('nest_id'); 
     $table->string('inviteEmail'); 
     $table->timestamps(); 
    }); 
} 

는이 같은 사용자 ID에 따라이 문제를 할 수 없다 : 여기 내 scehma입니다

Route::get('/t1', function() { 

    $nest = User::find(2)->nest()->where('inviteEmail', '=', '[email protected]')->get(); 

    foreach($nest as $nest) 

    echo $nest->name, ': ', $nest->pivot->inviteEmail, "</br>"; 
}); 

내가 둥지와 이름과 피벗에서 이메일 얻을 수 있습니다 - 최고를 .. 그러나, 나는 사용자 ID에 묶이지 않은 관련 이메일을 가진 모든 '둥지'를 찾고 싶다. 이 가까이 저를지고 있지만 여전히 작동하지 않습니다 :

Route::get('/t4', function() { 

    $users = User::all(); 

    foreach($users as $users) 
    { 
     $nests = $users->with(array('nest' => function($query) { 
     $query->where('inviteEmail', '=', '[email protected]'); 
    }))->get(); 

     foreach($nests->nest as $nest) 
     { 
      echo $nest->name,"<br />"; 

     } 
    } 

    }); 

나는이 오류가 무엇입니까 :

Undefined property: Illuminate\Database\Eloquent\Collection::$nest 
+0

합니다. 이것은 내가 Laravel과 가진 마지막 큰 논리/문법 장애물입니다. 전 문서를 기반으로 모든 구성을 시도했지만 아직 성공하지 못했습니다. 어떤 통찰력도 좋을 것입니다. 나는 너무 가까이에 있다고 느낍니다. – Danaia

답변

0

을 나는 완전히 질문을 이해 모르겠지만, 마지막 코드 블록은 '아무튼 말이 되네. 사용자를 인출하는 동안 함께 할 수 있습니다. 또한 $ nests (Collection)에는 $ nest 속성이 없으므로 가져 오는 오류가 적절합니다.

는 다시, 나는이 필요하시면 모르겠지만,이 시도 제공 : 나는 누군가가 도움이 될 수 있습니다 희망

Route::get('/t4', function() { 

    // Get all users and their nests where nests are filtered by inviteEmail 
    $users = User::with(array('nest' => function($query) { 
     $query->where('inviteEmail', '=', '[email protected]'); 
    }))->get(); 

    // Loop through all users 
    foreach($users as $user) 
    { 
     // I'm asuming you defined the relation as nests() in your User model. 
     foreach($user->nests as $nest) 
     { 
      echo $nest->name . "<br />"; 
     } 
    } 
}); 
+0

고마워요 ...이 작동했지만 피벗 테이블 열에 액세스하려고하지만이 솔루션은 중첩 테이블에 액세스하는 것처럼 보입니다. 논리를 리팩토링 할 수 있었고 코드를 사용하여 네스트 테이블에서 작업 할 수있었습니다. 감사! – Danaia