Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use laravel hasManyThrough

Shulou Source: shulou.com Published: 2022-06-01 23:50:17 10月01日 Update

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!

Tags: Parameters hubs goals associations fields articles situations tables countries posts users class names test papers questions queries values interests and more friends Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Shulou Information Microsoft vpn Xiaomi