php - 雄辩关系上的 toSql() 返回`where id = null`
问题描述
我正在研究 3 维数据透视表
建筑学
我制作了以下表格和模型。
register_colleges( model: RegisterCollege )
- college_id
- random field
programs ( model: Program )
- id
- program_name
courses ( model: Course )
- id
- course_name
course_program_register_college
- course_id
- program_id
- college_id
现在我面临的问题是,null
当我尝试访问program_name
(比如说)时,我的视图会显示结果。这是我所期望的,因为当我将其转换为 Sql() 时,它会显示以下查询 -
select * from 'programs' inner join 'course_program_register_college' on 'programs'.'id' = 'course_program_register_college'.'program_id' 其中 'course_program_register_college'.'college_id' 为空
我无法弄清楚为什么这个查询试图获取数据where 'course_program_register_college'.'college_id' is null
。
RegisterCollege.php(模型)
// pivot table is looking for register_college_id as a primary key field
protected $primarykey = 'college_id';
public function programs(){
return $this->belongsToMany('App\Program',
'course_program_register_college', 'college_id');
}
编辑:-
雄辩的查询
{{ $college->programs()->toSql() }} // in some view.blade.php
解决方案
推荐阅读
- php - 是什么导致此 Codeigniter 3 应用程序中的错误“无法在写入上下文中使用方法返回值”?
- javascript - 什么时候应该在 JS 中而不是在 HTML 中编写链接?
- html - 如何解析html
弦在颤动 - javascript - 谁能告诉我为什么我突然面临这个问题?
- c# - 导航管理器 NullReferenceException
- ios - 圆角按钮被切断的问题
- javascript - 如何使用 xpath 回显属性
- php - laravel 7中没有显示背景颜色
- python - 错误,文件“
",第 12 行返回 random.choice(bots_greeting) ^ SyntaxError: 'return' outside function - google-apps-script - 如何在 Google Apps 脚本中将 Google 表单响应目标设置为两个不同的电子表格?