Home > Enterprise >  Illuminate\Database\Eloquent\Relations\HasOne::$section_id
Illuminate\Database\Eloquent\Relations\HasOne::$section_id

Time:08-24

I have an observer that i want to output message wherever the enrollment has section in it.

I am not sure if this is enough information. Please do not hesitate to ask me to provide more.

UPDATED 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

student.sections
--------------
id
student_id
section_id
enrollment_id

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
date_dropped
drop_reason
public function created(Enrollment $enrollment)
    {
        $enrollmentSection = $enrollment->studentSection()->section_id;
        $enrollment = $enrollment->student()->get()->first();
        if($enrollmentSection != ''){
           Log::info($enrollmentSection);
           Logger::createLog("Assigned Section '" . $enrollment->last_name . ", " . $enrollment->first_name . "'");
        }
        Logger::createLog("Enrolled Student '" . $enrollment->last_name . ", " . $enrollment->first_name . "'");
    }

But this gives me error of undefined property

[2022-08-23 03:00:06] local.ERROR: Undefined property: Illuminate\Database\Eloquent\Relations\HasOne::$section_id {"userId":1,"exception":"[object] (ErrorException(code: 0): Undefined property: Illuminate\\Database\\Eloquent\\Relations\\HasOne::$section_id at /application/app/Observers/EnrollmentObserver.php:20)
[stacktrace]

Here's my relationship for enrollement

class Enrollment extends Model
{
    use HasFactory;
    
    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');
    }
}

And here's my student section in App\Models\Student\Section

class Section extends Model
{
    use HasFactory;
    
    protected $table = 'student.sections';
    protected $fillable = [
        'student_id',
        'section_id',
        'enrollment_id',
    ];
    public function student()
    {
        return $this->belongsTo('App\Models\Student', 'student_id');
    }

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

    public function enrollment()
    {
        return $this->belongsTo('App\Models\Enrollment', 'enrollment_id');
    }
}

CodePudding user response:

You should not be using the function studentSection() because that will return the query object and not the model. Therefore section_id does not exist. I think the code below will fix your problem.

$enrollmentSection = $enrollment->studentSection->section_id;

CodePudding user response:

I've solved it. It seems i was logging in enrollmentObserver but it was actually getting triggered in studentSectionObserver. Sorry for the confusion i've caused.

  • Related