首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取当前日期的学生数量- laravel 8

获取当前日期的学生数量- laravel 8
EN

Stack Overflow用户
提问于 2022-08-31 05:24:53
回答 2查看 39关注 0票数 0

我有一个方法,我想逐段获取程序。

我有条款和会话,模型程序和部门的枢轴表,program.sections,然后添加术语和program.sections到进度表,以登记学生在招生表。

DB模式

代码语言:javascript
复制
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

登记控制器

代码语言:javascript
复制
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);
    }

这是我在节目部分的关系

代码语言:javascript
复制
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('App\Models\Session', 'session_id');
    }

    public function academicLevel()
    {
        return $this->belongsTo('App\Models\AcademicLevel', 'academic_level_id');
    }

    public function program()
    {
        return $this->belongsTo('App\Models\Program', 'program_id');
    }

    public function user()
    {
        return $this->belongsTo('App\Models\User', 'user_id');
    }

    public function section()
    {
        return $this->belongsTo('App\Models\Section', 'section_id');
    }

    public function schedules()
    {
        return $this->hasMany('App\Models\Schedule', 'section_id', 'id');
    }

    public function classRecord()
    {
        return $this->hasOne("App\Models\Grading\ClassRecord");
    }

    public function students()
    {
        return $this->hasManyThrough(
            'App\Models\Student',
            'App\Models\Student\Section',
            'section_id',
            'id',
            'id',
            'student_id',
        );
    }

招生模型

代码语言:javascript
复制
protected $table = 'enrollment';
protected $fillable = [
        'session_id',
        'student_id',
        'program_id',
        'academic_level_id',
        'date_dropped',
        'drop_reason',
    ];

    public function session()
    {
        return $this->belongsTo('App\Models\Session', 'session_id');
    }

    public function student()
    {
        return $this->belongsTo('App\Models\Student', 'student_id');
    }

    public function program()
    {
        return $this->belongsTo('App\Models\Program', 'program_id');
    }

    public function academicLevel()
    {
        return $this->belongsTo('App\Models\AcademicLevel', 'academic_level_id');
    }

    public function programSection()
    {
        return $this->belongsTo('App\Models\Program\Section', 'session_id', 'session_id');
    }

    public function studentSection()
    {
        return $this->hasOne('App\Models\Student\Section');
    }

    public function enrollmentFee()
    {
        return $this->hasMany('App\Models\Accounting\EnrollmentFee', 'enrollment_id', 'id');
    }

    public function studentLedger()
    {
        return $this->hasMany('App\Models\Student\Ledger');
    }

    public function lastLedger()
    {
        return $this->hasOne('App\Models\Student\Ledger')->latest();
    }

会话模型

代码语言:javascript
复制
protected $table = 'sessions';
    protected $fillable = [
        'term_id',
        'start_date',
        'end_date',
        'session_type',
    ];

    public function term()
    {
        return $this->belongsTo('App\Models\Term', 'term_id');
    }

    public function schedules()
    {
        return $this->hasMany('App\Models\Schedule', 'session_id', 'id');
    }

    public function enrollments()
    {
        return $this->hasMany('App\Models\Enrollment', 'session_id', 'id');
    }

    public function sessionFees()
    {
        return $this->hasMany('App\Models\Accounting\SessionFee', 'session_id', 'id');
    }

    public function getAllowDeleteAttribute()
    {
        return !($this->schedules()->exists() || $this->sessionFees()->exists() || $this->enrollments()->exists());

    }

这是我用我的方法得到的错误。我不太熟悉雄辩的质疑,这就是为什么我现在被困在这里3个小时了。

代码语言:javascript
复制
message: "Call to undefined method App\\Models\\Program\\Section::enrollment()"

请不要犹豫,请我提供更多的信息。

EN

回答 2

Stack Overflow用户

发布于 2022-08-31 05:33:27

您可以使用Carbon::today()->toDateString()并将其与您的created_at字段匹配。

票数 0
EN

Stack Overflow用户

发布于 2022-09-02 20:58:35

代码语言:javascript
复制
$programSection = ProgramSection::with(...)
    ->withCount(['enrollment ...' => function ($query) {
        // ...
}])

您可以在ProgramSection模型上调用“注册”,在您的示例中,不存在。因此出现了错误:

代码语言:javascript
复制
message: "Call to undefined method App\\Models\\Program\\Section::enrollment()"

请先定义这种方法/关系。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73551201

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档