2017-09-03 2 views
0

좋은 하루. 내 데이터베이스에 쿼리하여 자녀의 하위 항목을 가져 오려고합니다. 모든 사용자에게는 2 명의 자녀가 있습니다. 쿼리 작성기를 사용하고 있습니다. 요구 사항은 웅변적이고 웅변적인 관계를 사용하지 않는 것입니다. 그러나 그것으로 고투하고있다.데이터베이스 laravel의 다른 레벨 쿼리

$firstchild= DB::table('users') - >where('parent_id', Auth::user() ->id) -> get() ; 
$secondchild1 = DB::table('users') - >where('parent_id', $firstchild[0]->parent_id) -> get() ; 
$secondchild2 = DB::table('users') - >where('parent_id', $firstchild[1]parent_id) -> get() ; 
return view('home' ['firstchild' => $firstchild, 'secondchild1 ' => $secondchild1, 'secondchild2 ' => $secondchild2 , ]) 

사용자 자식에 자식이 없으면 정의되지 않은 오프셋 0을 반환합니다. 오류가 발생하지 않도록하려면 어떻게해야합니까?

쿼리 결과가 제공 한 해당 하위 항목의 하위 항목을 가져 오려면 어떻게해야합니까?

답변

1

이 시도 :

$firstchild = DB::table('users')->where('parent_id', Auth::user()->id)->get(); 

if ($firstchild->count() == 2) { //**Contains exactly 2 arrays inside the 'firstchild' collection. 
    $secondchild1 = DB::table('users')->where('parent_id', $firstchild[0]->parent_id)->get(); 
    $secondchild2 = DB::table('users')->where('parent_id', $firstchild[1]->parent_id)->get(); 
} 

return view('home', compact('firstchild', 'secondchild1', 'secondchild2')); 

그것이 도움이 바랍니다.

+0

감사합니다. 그 사람이 단 하나의 자식이 있다면 그것은 정의되지 않은 오프셋 1을 반환합니다. –

+0

자녀의 자식과 그 자식을 얻는 방법을 내게 보여 주면 감사 하겠지만 –

+0

DB 스키마 표시 –

관련 문제