我有一个方法,我想按会话获取程序部分。
我有条款&会话模型程序&透视表部分program.sections然后添加术语&program.时间表表中的部分,以便在注册表中注册学生。
数据库模式
terms
-----
id
name
start_date
end_date
term_type
active
sessions
--------
id
term_id
start_date
end_date
session_type
students
--------
id
first_name
last_name
middle_name
suffix
email
student_number
birthdate
sex
lrn
profile_picture
programs
--------
id
name
code
description
sections
--------
id
name
code
section_terms
-------------
section_term_id
term_id
section_id
program.sections
--------------
id
session_id
academic_level_id
user_id
section_id
max_students
program_id
program.subjects
--------------
id
subject_id
program_id
academic_level_id
semester_level
academic_levels
---------------
id
code
name
description
ordering
enrollment
----------
id
student_id
session_id
program_id
academic_level_id
注册控制器
public function countStudentEnrollmentBySexAndProgramSection()
{
$programSection = ProgramSection::with('session')
->withCount(['enrollment as female_students' => function ($query) {
$query->whereHas('student', function ($query) {
$query->where('sex', Student::FEMALE);
});
}])->withCount(['enrollment as male_students' => function ($query) {
$query->whereDate('created_at', Carbon::now());
$query->whereHas('student', function ($query) {
$query->where('sex', Student::MALE);
});
}])->get();
return response()->json($programSection, 200);
}
这是我与程序部分的关系
protected $table = 'program.sections';
protected $fillable = [
'session_id',
'academic_level_id',
'user_id',
'section_id',
'max_students',
'program_id',
];
public function session()
{
return $this->belongsTo('AppModelsSession', 'session_id');
}
public function academicLevel()
{
return $this->belongsTo('AppModelsAcademicLevel', 'academic_level_id');
}
public function program()
{
return $this->belongsTo('AppModelsProgram', 'program_id');
}
public function user()
{
return $this->belongsTo('AppModelsUser', 'user_id');
}
public function section()
{
return $this->belongsTo('AppModelsSection', 'section_id');
}
public function schedules()
{
return $this->hasMany('AppModelsSchedule', 'section_id', 'id');
}
public function classRecord()
{
return $this->hasOne("AppModelsGradingClassRecord");
}
public function students()
{
return $this->hasManyThrough(
'AppModelsStudent',
'AppModelsStudentSection',
'section_id',
'id',
'id',
'student_id',
);
}
注册模型
protected $table = 'enrollment';
protected $fillable = [
'session_id',
'student_id',
'program_id',
'academic_level_id',
'date_dropped',
'drop_reason',
];
public function session()
{
return $this->belongsTo('AppModelsSession', 'session_id');
}
public function student()
{
return $this->belongsTo('AppModelsStudent', 'student_id');
}
public function program()
{
return $this->belongsTo('AppModelsProgram', 'program_id');
}
public function academicLevel()
{
return $this->belongsTo('AppModelsAcademicLevel', 'academic_level_id');
}
public function programSection()
{
return $this->belongsTo('AppModelsProgramSection', 'session_id', 'session_id');
}
public function studentSection()
{
return $this->hasOne('AppModelsStudentSection');
}
public function enrollmentFee()
{
return $this->hasMany('AppModelsAccountingEnrollmentFee', 'enrollment_id', 'id');
}
public function studentLedger()
{
return $this->hasMany('AppModelsStudentLedger');
}
public function lastLedger()
{
return $this->hasOne('AppModelsStudentLedger')->latest();
}
会话模型
protected $table = 'sessions';
protected $fillable = [
'term_id',
'start_date',
'end_date',
'session_type',
];
public function term()
{
return $this->belongsTo('AppModelsTerm', 'term_id');
}
public function schedules()
{
return $this->hasMany('AppModelsSchedule', 'session_id', 'id');
}
public function enrollments()
{
return $this->hasMany('AppModelsEnrollment', 'session_id', 'id');
}
public function sessionFees()
{
return $this->hasMany('AppModelsAccountingSessionFee', 'session_id', 'id');
}
public function getAllowDeleteAttribute()
{
return !($this->schedules()->exists() || $this->sessionFees()->exists() || $this->enrollments()->exists());
}
这是我的方法出现的错误。我不太熟悉雄辩的疑问,这就是为什么我现在被这个问题困扰了3个小时。
message: "Call to undefined method App\Models\Program\Section::enrollment()"
请不要犹豫,请我提供更多信息。
您可以使用Carbon::today()->toDateString()
并将其与created_at字段相匹配。
$programSection = ProgramSection::with(...)
->withCount(['enrollment ...' => function ($query) {
// ...
}])
您在ProgramSection
模型上调用‘enrollment’,在您的情况下,并不存在。因此出现错误:
message: "Call to undefined method App\Models\Program\Section::enrollment()"
请先定义该方法/关系。