下面由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中文網其他相關文章!

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

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

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

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

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

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

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


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

禪工作室 13.0.1
強大的PHP整合開發環境

WebStorm Mac版
好用的JavaScript開發工具

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

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