查询数据库中的不同级别 laravel
Query different levels in database laravel
美好的一天。我正在尝试查询我的数据库以获取 child 的 child 的 child。每个用户有2children。我正在使用查询生成器。要求是不要使用 eloquent 和 eloquent 关系。但是我正在努力。
$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 , ])
如果用户 child 没有 child,它 returns 未定义偏移量 0。如果我想没有任何错误,我该怎么做。
如果我想得到查询结果给出的children中的children,我该怎么做?
试试这个:
$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'));
希望对您有所帮助。
美好的一天。我正在尝试查询我的数据库以获取 child 的 child 的 child。每个用户有2children。我正在使用查询生成器。要求是不要使用 eloquent 和 eloquent 关系。但是我正在努力。
$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 , ])
如果用户 child 没有 child,它 returns 未定义偏移量 0。如果我想没有任何错误,我该怎么做。
如果我想得到查询结果给出的children中的children,我该怎么做?
试试这个:
$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'));
希望对您有所帮助。