Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use laravel hasManyThrough

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use laravel hasManyThrough, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

In the first case, I call it a conduction correlation table (simple schema)

There are many users in the country, and users have many posts

Countries id-integer name-stringusers id-integer country_id-integer name-stringposts id-integer user_id-integer title-string

Query all posts in a country, how to achieve?

Countries is the table, posts is the target table to be exported, and users is the intermediate table

Return $this- > hasManyThrough ('App\ Post',' App\ User', 'country_id',' user_id')

In the second case, there is an intermediate table (pure intermediate table)

Exam_paper (examination paper table) idnameexam_paper_question (intermediate list of examination papers and questions) idexam_paper_idquestion_idexam_question (examination paper table) idname

We need to query question through exam_paper 's id

Return $this- > hasManyThrough ('exam_question',' exam_paper_question', 'exam_paper_id',' id','id','question_id') / Parameter 1 target table class name exam_question,// parameter 2 hub table class name exam_paper_question,// parameter 3 field name associated with the current table 'exam_paper_question.exam_paper_id',// parameter 4 field name associated with the hub table' exam_question.id',// parameter 5 field name associated with the hub table in the current table 'exam_paper.id' / / Parameter 6 the field name associated with the hub table and the target table is' exam_paper_question.question_id')

If you record the current table as A, the target table as B, and the intermediate table as CMagol 6 parameters (BMague CMagna CA BCMagol CB)

Thank you for reading this article carefully. I hope the article "how to use laravel hasManyThrough" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report