### ***一.关联预载入***
1.在普通的关联查询下:我们循环数据列表会执行n+1次SQL查询
$list = UserModel::all(\[1,2,3\]);
foreach($list as $user){
dump($user->profile);
}
***2.上面继续采用一对一的构建方式,打开trace调试工具,会得到四次查询;***
***3.如果采用关联预载入的方式,将会减手到两次,也就是起步一次,循环一次***
$list = UserModel::with('profile')->all(\[1,2,3\]);
foreach($list as $user){
dump($user->profile)
}
***with()是IN方式的查询,如果想使用JOIN查询,可以使用withJoin();***
UserModel::withJoin('profile')->all(\[1,2,3\]);
在使用JOIN查询方案下,限定字段,可以使用withField()方法来进行
$list = UserModel::withJoin(\['profile'=>function($query){
$query->withField('hobby');
}\])->all(\[1,2,3\]);
***关联预载入还提供了一个延迟预载入,就是先执行all()再load()载入,***
这个也就是说,在载入前,去延迟一下,然后在延迟的时间中还可以去做一些其他的操作
$list = UserModel::all(\[1,2,3\]);//先执行all查询
//可以做一些操作
$list->**load**('profile');//**延迟预加载**
foreach($list as $user){
dump($user->profile);
}