Php 使用laravel-eager加载选择特定列不起作用
当我试图返回特定的列时,我真的不明白为什么这种急切的加载不起作用。 这段代码工作得很好Php 使用laravel-eager加载选择特定列不起作用,php,sql,laravel,eloquent,Php,Sql,Laravel,Eloquent,当我试图返回特定的列时,我真的不明白为什么这种急切的加载不起作用。 这段代码工作得很好 $user=User::where('userName', $userName) ->with('points') ->with('examstaken') ->with('question') /*->with(array('points'=>function($query){ $q
$user=User::where('userName', $userName)
->with('points')
->with('examstaken')
->with('question')
/*->with(array('points'=>function($query){
$query->select('id','points');
}))*/
->get();
这个密码不正确
$user=User::where('userName', $userName)
/*->with('points')*/
->with('examstaken')
->with('question')
->with(array('points'=>function($query){
$query->select('id','points');
}))
->get();
不起作用意味着,“点”不返回任何值,而第一个点正确返回值
知道有什么问题吗
这是我的用户模型中的关系
public function points()
{
return $this->hasMany('App\Points','user_id');
}
谢谢 好的,解决了。似乎我必须传递用户id和外键,然后他们才能工作。将其作为答案发布,因为其他人可能会获得帮助:
$user=User::where('userName', $userName)
/*->with('points')*/
->with('examstaken')
->with('question')
->with(array('points'=>function($query){
$query->select('user_id','points');
}))
->get();
好的,解决了。似乎我必须传递用户id和外键,然后他们才能工作。将其作为答案发布,因为其他人可能会获得帮助:
$user=User::where('userName', $userName)
/*->with('points')*/
->with('examstaken')
->with('question')
->with(array('points'=>function($query){
$query->select('user_id','points');
}))
->get();
如果您没有选择关系所需的键,Eloquent将无法按键将子项与其父母匹配如果您没有选择关系所需的键,Eloquent将无法按键将子项与其父母匹配