搜尋
首頁php框架Laravel解析laravel hasManyThrough如何用?

下面由Laravel教學專欄跟大家介紹laravel hasManyThrough用法及參數,希望對大家有幫助!

第一種情況,我稱之為傳導關聯表(簡單模式)

國家有很多用戶,用戶有很多貼文

countries
    id - integer
    name - string

users
    id - integer
    country_id - integer
    name - string

posts
    id - integer
    user_id - integer
    title - string

查詢某個國家的所有帖子,怎麼實現?

countries為本表,posts為要輸出的目標表,users為中間表

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

第二種情況,有中間表情況(純中間表)

exam_paper(试卷表)id
nameexam_paper_question(试卷与试题中间表)id
exam_paper_id
question_idexam_question(试题表)id
name

我們要透過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如何用?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:learnku。如有侵權,請聯絡admin@php.cn刪除
最新的Laravel版本:DIFES DISCON最新的Laravel版本:DIFES DISCONMay 12, 2025 am 12:15 AM

Laravel10IntroducessEveralKeyFeatUrestHatenHanceWebDevelopment.1)LazyCollectionsAllyCollefficeProcesingOflargeFlargedAtasetSwithSwithOutloadingAllRecordSintomeMemory.2)the Make:Model Model Moged-and-Mogration'ArtisanCommandSancancMommandSimplififieScreatingModeltigation.3)

Laravel遷移解釋了:創建,修改和管理您的數據庫Laravel遷移解釋了:創建,修改和管理您的數據庫May 12, 2025 am 12:11 AM

laravelmigrationssshouldbeusedbecausetheystreamlinedeplupment,nesurecresistencyAcrossenviments和simplifyCollaborationAndDeployment.1)shemallogragrammatonofdatabaseschemachanges,ReeducingErrors.2)MigrigationScanBeverCanbeverSionConconconconcontrollin.2)

Laravel遷移:值得使用嗎?Laravel遷移:值得使用嗎?May 12, 2025 am 12:10 AM

是的。

Laravel:軟刪除了性能問題Laravel:軟刪除了性能問題May 12, 2025 am 12:04 AM

SoftDeletesinLaravelimpactperformancebycomplicatingqueriesandincreasingstorageneeds.Tomitigatetheseissues:1)Indexthedeleted_atcolumntospeedupqueries,2)Useeagerloadingtoreducequerycount,and3)Regularlycleanupsoft-deletedrecordstomaintaindatabaseefficie

Laravel遷移對什麼有益?用例和福利Laravel遷移對什麼有益?用例和福利May 11, 2025 am 12:14 AM

Laravelmigrationsarebeneficialforversioncontrol,collaboration,andpromotinggooddevelopmentpractices.1)Theyallowtrackingandrollingbackdatabasechanges.2)Migrationsensureteammembers'schemasstaysynchronized.3)Theyencouragethoughtfuldatabasedesignandeasyre

如何在Laravel中使用軟刪除:保護您的數據如何在Laravel中使用軟刪除:保護您的數據May 11, 2025 am 12:14 AM

Laravel的軟刪除功能通過標記記錄而非實際刪除來保護數據。 1)在模型中添加SoftDeletestrait和deleted_at字段。 2)使用delete()方法標記刪除,使用restore()方法恢復。 3)查詢時使用withTrashed()或onlyTrashed()包含軟刪除記錄。 4)定期清理超過一定時間的軟刪除記錄以優化性能。

Laravel遷移是什麼,您如何使用它們?Laravel遷移是什麼,您如何使用它們?May 11, 2025 am 12:13 AM

laravelmigrationSareversionControlfordatabaseschemas,允許Roducibleandreversiblechanges.tousethem:1)creatighatsanmake:遷移',2)定義chemachangesinthe'up()

Laravel遷移:回滾行不通,發生了什麼事?Laravel遷移:回滾行不通,發生了什麼事?May 11, 2025 am 12:10 AM

Laravelmigrationsmayfailtorollbackduetodataintegrityissues,foreignkeyconstraints,orirreversibleactions.1)Dataintegrityissuescanoccurifamigrationaddsdatathatcan'tbeundone,likeacolumnwithadefaultvalue.2)Foreignkeyconstraintscanpreventrollbacksifrelatio

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)