我有一个观察者,我想输出消息,只要注册在其中的部分。
我不知道这些资料是否足够。请不要犹豫,请我提供更多。
更新的数据库模式
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_reasonpublic 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 . "'");
}但这给我带来了未定义属性的错误。
[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]这是我申请入学的关系
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');
}
}这是我在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');
}
}发布于 2022-08-24 05:43:40
我已经解决了。我似乎登录了enrollmentObserver,但它实际上是在studentSectionObserver中触发的。对我造成的混乱很抱歉。
发布于 2022-08-23 03:23:11
您不应该使用函数studentSection(),因为这将返回查询对象,而不是模型。因此,section_id不存在。我认为下面的代码会解决你的问题。
$enrollmentSection = $enrollment->studentSection->section_id;https://stackoverflow.com/questions/73452763
复制相似问题