首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Illuminate\Database\Eloquent\Relations\HasOne::$section_id

Illuminate\Database\Eloquent\Relations\HasOne::$section_id
EN

Stack Overflow用户
提问于 2022-08-23 03:16:31
回答 2查看 59关注 0票数 0

我有一个观察者,我想输出消息,只要注册在其中的部分。

我不知道这些资料是否足够。请不要犹豫,请我提供更多。

更新的数据库模式

代码语言:javascript
复制
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
代码语言:javascript
复制
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 . "'");
    }

但这给我带来了未定义属性的错误。

代码语言:javascript
复制
[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]

这是我申请入学的关系

代码语言:javascript
复制
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的学生区

代码语言:javascript
复制
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');
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-08-24 05:43:40

我已经解决了。我似乎登录了enrollmentObserver,但它实际上是在studentSectionObserver中触发的。对我造成的混乱很抱歉。

票数 1
EN

Stack Overflow用户

发布于 2022-08-23 03:23:11

您不应该使用函数studentSection(),因为这将返回查询对象,而不是模型。因此,section_id不存在。我认为下面的代码会解决你的问题。

代码语言:javascript
复制
$enrollmentSection = $enrollment->studentSection->section_id;
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73452763

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档