下面由laravel教程栏目给大家介绍laravel hasmanythrough用法及参数,希望对大家有所帮助!
第一种情况,我称之为传导关联表(简单模式)
国家有很多用户,用户有很多帖子
countries id - integer name - stringusers id - integer country_id - integer name - stringposts id - integer user_id - integer title - string
查询某个国家的所有帖子,怎么实现?
countries为本表,posts为要输出的目标表,users为中间表
return $this->hasManyThrough('AppPost', 'AppUser', 'country_id', 'user_id');
第二种情况,有中间表情况(纯中间表)
如知AI笔记
如知笔记——支持markdown的在线笔记,支持ai智能写作、AI搜索,支持DeepseekR1满血大模型
27 查看详情
exam_paper(试卷表)idnameexam_paper_question(试卷与试题中间表)idexam_paper_idquestion_idexam_question(试题表)idname
我们要通过exam_paper的id查询question
return $this->hasManyThrough('exam_question', 'exam_paper_question', 'exam_paper_id', 'id','id','question_id');
// 参数1 目标表类名exam_question,// 参数2 枢纽表类名exam_paper_question,// 参数3 枢纽表中和当前表关联的字段名'exam_paper_question.exam_paper_id',// 参数4 目标表和枢纽表关联的字段名'exam_question.id',// 参数5 当前表中和枢纽表关联的字段名'exam_paper.id',// 参数6 枢纽表和目标表关联的字段名'exam_paper_question.question_id');
如果把当前表记作A,目标表记作B,中间表记作C,6个参数记作(B,C,CA,BC,AC,CB)
推荐学习:《laravel视频教程》
以上就是解析laravel hasManyThrough如何用?的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/343128.html
微信扫一扫
支付宝扫一扫