通过Laravel中的模型函数查询结果
我相信这应该很简单,但我不太明白。这是拉威尔5.4 我有三个模型:简介、评估和回答。每个概要文件都有许多评估,每个评估都有许多响应 我想能够得到的档案,其中有一个最新的评估,其中总分为0;我假设我可以在一个雄辩的查询中以某种方式使用我的模型函数(latestAssessment()和total_points())。比如:通过Laravel中的模型函数查询结果,laravel,laravel-5,eloquent,Laravel,Laravel 5,Eloquent,我相信这应该很简单,但我不太明白。这是拉威尔5.4 我有三个模型:简介、评估和回答。每个概要文件都有许多评估,每个评估都有许多响应 我想能够得到的档案,其中有一个最新的评估,其中总分为0;我假设我可以在一个雄辩的查询中以某种方式使用我的模型函数(latestAssessment()和total_points())。比如: $noscore_profiles = \App\Profile::where('latestAssessment', function($query) { $
$noscore_profiles = \App\Profile::where('latestAssessment', function($query) {
$query->where(total_points(), '=', 0);
})->paginate(2, ['*'], 'noscore_profiles');
我已经尝试了很多方法来做这件事,但是我的脑袋还是绕不过去
个人资料:
class Profile extends Model
{
protected $fillable = ['firstname', 'surname', 'gender', 'height', 'telephone', 'extension', 'start_date', 'yearly_reminder_sent'];
public function user() {
return $this->belongsto('App\User');
}
public function assessments() {
return $this->hasMany('App\Assessment');
}
public function latestAssessment() {
return $this->hasOne('App\Assessment')->latest();
}
public function responses() {
return $this->hasManyThrough('App\Response', 'App\Assessment');
}
public function total_points() {
$total_points = 0;
foreach($this->responses as $response) {
$total_points += $response->answer->points;
}
return $total_points;
}
}
$noscore_profiles = \App\Profile::whereHas('latestAssessment', function($assessment) {
$assessment->select('assessments.id','assessments.profile_id')
->join('responses','responses.assessment_id', '=', 'assessments.id')
->join('answers', 'answers.id', '=', 'responses.answer_id')
->groupBy('assessments.id','assessments.profile_id')
->havingRaw('SUM(answers.points) = 0');
})->paginate(2, ['*'], 'noscore_profiles');
评估
class Assessment extends Model
{
protected $fillable = ['profile_id', 'start_date', 'completion_date', 'completion_status'];
public function profile()
{
return $this->belongsTo('App\Profile');
}
public function review()
{
return $this->hasOne('App\Review');
}
public function responses()
{
return $this->hasMany('App\Response');
}
public function equipment_requests()
{
return $this->hasMany('App\EquipmentRequests');
}
public function total_points() {
$total_points = 0;
foreach($this->responses as $response) {
$total_points += $response->answer->points;
}
return $total_points;
}
}
已回答-最后有效的代码:
class Profile extends Model
{
protected $fillable = ['firstname', 'surname', 'gender', 'height', 'telephone', 'extension', 'start_date', 'yearly_reminder_sent'];
public function user() {
return $this->belongsto('App\User');
}
public function assessments() {
return $this->hasMany('App\Assessment');
}
public function latestAssessment() {
return $this->hasOne('App\Assessment')->latest();
}
public function responses() {
return $this->hasManyThrough('App\Response', 'App\Assessment');
}
public function total_points() {
$total_points = 0;
foreach($this->responses as $response) {
$total_points += $response->answer->points;
}
return $total_points;
}
}
$noscore_profiles = \App\Profile::whereHas('latestAssessment', function($assessment) {
$assessment->select('assessments.id','assessments.profile_id')
->join('responses','responses.assessment_id', '=', 'assessments.id')
->join('answers', 'answers.id', '=', 'responses.answer_id')
->groupBy('assessments.id','assessments.profile_id')
->havingRaw('SUM(answers.points) = 0');
})->paginate(2, ['*'], 'noscore_profiles');
您不能在查询中使用
total_points()
,因为这需要调用一个实例,并且您需要查询结果来实例化模型
您可以做的是将逻辑放入查询本身
$noscore\u profiles=\App\Profile::whereHas('latestatassessment',函数($assessment){
$assessment->whereRaw('SUM(answers.points)=0')
->加入('responses'、'response.assessment_id'、'='、'assessments.id')
->join('answers','answers.response_id','=','responses.if')
->groupBy('assessments.id'、'profile\u id')
->选择('assessments.id','assessments.profile_id');
})->paginate(2,['*'],'noscore_profiles');
您不能在查询中使用total_points()
,因为这需要调用一个实例,您需要查询结果来实例模型。感谢您的回复,我理解我不能使用该函数,但是,您编写的查询返回未知列“我尝试修改的总积分->selectRaw”(“assessments.id,SUM(answers.points)as total_points)的错误;但是这仍然返回一个未知列错误。@NickoBrooko哦,我的错。忘了在SumThank上设置别名,不幸的是,更新后的代码仍然出现错误:SQLSTATE[42S22]:未找到列:1054未知列“where子句”中的“total_points”(SQL:select count(*)作为聚合自profiles
where exists(select*)、assessments.id、SUM(answers.points)作为评估
内部连接响应
上的评估
评估评估的总分id内部连接回答
上的响应
响应如果
其中配置文件
id<>评估
profile\u id
和total\u points
=0 group by评估
id
)@NickoBrooko我已将聚合直接放在where上。这看起来更好了,我现在因为无效使用group函数而得到一个SQL错误。还-需要编辑响应。如果是响应。id一般错误:1111组函数的使用无效(SQL:选择count(*)作为存在的配置文件的聚合(从assessment
internal joinresponses
onresponse
assessment\u id
assessments
id
internal joinanswers
response\u id
id
whereprofiles
idassessment评分个人资料(id
和总和(答案.分数)=0分组依据评估id
)