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.

Kael
  • 161
  • 2
  • 13

2 Answers2

0

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

Wakil Ahmed
  • 1,053
  • 1
  • 8
  • 16
  • Yes i have used carbon. My problem is that my method doesn't work it give 500 and undefined enrollment() – Kael Aug 31 '22 at 05:35
-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.

steven7mwesigwa
  • 5,701
  • 3
  • 20
  • 34