当我尝试返回特定列时,我真的不明白为什么这种预加载不起作用。 此代码完美运行
$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();
不起作用意味着,'points' 不返回任何值,而第一个正确返回值..
知道它有什么问题吗?
这是我的用户模型中的关系
public function points()
{
return $this->hasMany('App\Points','user_id');
}
谢谢..
最佳答案
好的解决了。看来我必须传递 user_id、外键,然后它们才能工作。将其作为答案发布,因为其他人可能会得到帮助:)
$user=User::where('userName', $userName)
/*->with('points')*/
->with('examstaken')
->with('question')
->with(array('points'=>function($query){
$query->select('user_id','points');
}))
->get();
关于php - 使用 laravel 预加载选择特定列不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47736572/