0

i have method that i want to get program section by session.

I have terms & sessions model program & section for pivot table program.sections then add terms & program.sections to schedules table to enroll student in enrollment table.

DB schema

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

Enrollmentcontroller

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);
    }

Here's my relationship for program section

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',
        );
    }

Enrollment model

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();
    }

Sessions model

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());

    }

This is the error i get with my method. Im not quite familiar with eloquent query that's why im getting stuck with this for 3hours now.

message: "Call to undefined method App\\Models\\Program\\Section::enrollment()"

Please do not hesitate to ask me to provide more information.

2 Answers 2

0

You can use Carbon::today()->toDateString() and match it with your created_at field.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes i have used carbon. My problem is that my method doesn't work it give 500 and undefined enrollment()
-1

$programSection = ProgramSection::with(...)
    ->withCount(['enrollment ...' => function ($query) {
        // ...
}])

You call 'enrollment' on the ProgramSection model, which in your case doesn't exist. Hence the error:

message: "Call to undefined method App\\Models\\Program\\Section::enrollment()"

Please, define that method/relationship first.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.