0

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');
    }
}
Kael
  • 161
  • 2
  • 13

2 Answers2

3

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;
amac
  • 921
  • 1
  • 6
  • 17
  • Hello bro thank you for taking time to answer. ```[2022-08-23 03:25:03] local.ERROR: Trying to get property 'section_id' of non-object```. i got this error. I don't understand i have this field in db. – Kael Aug 23 '22 at 03:26
  • Then your relationship returned nothing. As in the query didn't find the model it was looking for. – amac Aug 23 '22 at 03:27
  • I see but i got hasOne in enrollment and enrollment belongsTo. Sorry for this novice questions. – Kael Aug 23 '22 at 03:29
  • So the way you are defining the relationship is that the section table has an enrollment_id column that stores the id of the enrollment model. – amac Aug 23 '22 at 03:32
  • Yes bro. is this even the right path that i took or my query is all wrong. – Kael Aug 23 '22 at 03:35
  • Ya the path is fine but you are creating a dependency on section, but that is up to you. dump $enrollment->studentSection and post the results. – amac Aug 23 '22 at 03:39
  • I see i was overthinking after trying many things and finally arriving to this. i have dump the results bro but its doesn't log anything. Tried this ```$enrollmentSection = $enrollment->studentSection;``` but use ```Log::info($enrollmentSection);``` – Kael Aug 23 '22 at 03:43
  • actual result: ```[2022-08-23 03:45:10] local.INFO:``` – Kael Aug 23 '22 at 03:45
  • Well, two things. First, your code is breaking before you hit the log line. Also what you just showed is it didn't give you any results back. You should think about using something like tinkerwell to do this type of debugging faster. – amac Aug 23 '22 at 03:47
  • Tried using dd() to output more details but its not showing in laravel.log. Thanks i'll try what you suggested bro. – Kael Aug 23 '22 at 03:52
  • Why would it? dd doesn't write to the log file. Do something like `Log::info(json_encode($enrollmentSection));` – amac Aug 23 '22 at 03:55
  • ```[2022-08-23 03:57:06] local.INFO: null``` I see im not sure how to use dd in angular front end that's why i resorted to laravel.log i thought it can error there. I did what you've suggested bro – Kael Aug 23 '22 at 04:00
  • Ah, think I just saw the issue. Your table naming is not expected by laravel where it uses _ vs . So you will need to specify in your Enrollment model in your relationship for studentSection the Id. ```return $this->hasOne('App\Models\Student\Section', 'student_id');``` https://laravel.com/docs/9.x/eloquent-relationships#one-to-one – amac Aug 23 '22 at 06:04
  • ```[2022-08-23 06:15:03] local.INFO: null``` i dont get it already added the relationship. – Kael Aug 23 '22 at 06:17
  • 1
    Are you using this like a polymorphic relationship? Like do you have multiple section models that use the same table? Also I'm surprised you aren't bumping into namespacing issues or ambiguities. – amac Aug 23 '22 at 06:44
  • 1
    I definitely think it has to do with your naming conventions. Also you are creating extra relationships I'm not sure why you need to and it's getting more confusing the more I look at it. – amac Aug 23 '22 at 07:06
  • Yes bro. That's why im puzzled its actually created by our lead dev. It gets more confusing why it's not working even though we change the relationship id in model. – Kael Aug 23 '22 at 07:16
  • Ya I gotta bow out, too much. The naming is very convoluted and makes it more confusing. Like your program section is different than you student section by utilizing session_id, etc. Idk how your two sections are not the same schema... Anyway maybe try to create a listener or event than an observer. – amac Aug 23 '22 at 09:11
1

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.

Kael
  • 161
  • 2
  • 13