<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 一. Laravel 對象關聯關系 ## 1. 對象與數據庫表的關聯 Laravel有一個強大的數據庫工具ORM Eloquent,是每張數據表對應一個Model,對Model的操作就對應數據庫的操作,你只用管對model的操作,SQL語句則根據對象模型操作自動生成。 之前寫過一篇相關基礎介紹的文章:鏈接: Laravel model 模型用法和部分問題解方法 ## 2. 對象(表)之間的關聯關系 在我們代碼編寫過程中,對象之間有著多種的關聯關系; 一對一 例如:學生有唯一對應的班級 一對多 例如:班級有很多個學生 相應的在我們的表中對應的表現就是外鍵,通過外鍵與主鍵相等的方式,進行關系的建立,那么在Laravel中如何進行關聯查詢呢? # 二. 對象關系映射關聯關系的建立 ## 1. Model配置關聯關系 我們想要在后續的查詢中方便進行關聯查詢,首先我們要在Model中配置相關的關聯關系: //name是關聯關系的名稱,通常我們使用表名代替,方便記憶 ``` public function name() { //返回我們的關系,使用不同的方法進行關系建立 return $this->belongsTo('關聯對象', '關聯外鍵', '關聯主鍵'); } ``` ## 2. 關聯關系種類方法 我們這里以班級class,負責人principal,學生student作為參考。 ### hasOne 班級有一個負責人,那就在班級對象中,新增負責人關聯 ``` public function Principal(): \Illuminate\Database\Eloquent\Relations\BelongsTo { //hasOne( related:關聯的對象模型 , foreignKey:'關聯字段名') return $this->hasOne(Principal::class, 'class_id'); } ``` ### hasMany 一個班級有很多個學生,也就是一對多的關系,新增關聯 ``` public function student(): \Illuminate\Database\Eloquent\Relations\BelongsTo { //hasOne( related:關聯的對象模型 , foreignKey:'關聯字段名') return $this->hasMany(Student::class, 'class_id'); } ``` ### belongsTo 負責人負責一個班級,那就在負責人對象中,可以新增班級關聯 ``` public function class(): \Illuminate\Database\Eloquent\Relations\BelongsTo { //belongsTo( related:關聯的對象模型 , foreignKey:'關聯字段名')->->withDefault(); return $this->belongsTo(Class::class, 'principal_id')->withDefault(); } ``` ### withDefault: 正常情況下 負責人對應的班級是存在的,如果班級表中的數據刪除,那么關聯模型就會返回一個null值,避免所屬父級被刪除導致的異常問題 就是為了解決返回null所帶來問題的。 ### belongsToMany 但是假設學生可以報多個班級,那么學生和班級的關系就會變成多對多的關系,我們通常會使用一個中間的關系表,進行關聯 ``` public function class() { return $this->belongsToMany('目標對象','中間表','本表在中間表外鍵','目標表在中間表中外鍵','第三個參數如果對應的不是本表的主鍵則需要填寫,反之不需要'); } ``` ``` hasOneThrough ``` 遠程關聯一對一,比方說一個用戶擁有一個賬本,賬本中存在很多條消費記錄,我想從消費記錄中獲取到該用戶的信息,就需要遠程一對一 ``` public function record() { return $this->hasOneThrough('目標對象','中間表','本表在中間表外鍵','目標表在中間表中外鍵','第三個參數如果對應的不是本表的主鍵則需要填寫,反之不需要'); } ``` ### hasManyThrough ``` public function record() { return $this->hasManyThrough('目標對象名稱','中間表','本表在中間表外鍵','目標表在中間表中外鍵','當前表與中間表關聯字段名稱','中間表和目標表關聯的字段名稱'); } ``` # 三. 對象關系映射關聯關系的幾種方法 ## 1. with 介紹 with 是一種渴求式預加載,with 更像 sql 中的 join,無論關聯的結果中有沒有值都會返回,如果沒有就會返回一個空 使用方法 //with 中第一個參數就是我們上面建立關系的方法名 $query = Model::with('關系名稱')->get(); 1 2 我們如果想要在進行關聯查詢時,對其進行篩選,我們也可以在with中寫方法: //查詢所有的用戶,查詢條件:發布過標題中有first的post ``` $query = Model::with([ '關系名稱' => function ($query) { //對關聯表進行篩選,可以用where,orWhere 等 $query->where('title', 'like', '%first%'); } ])->get(); ``` 如果我們想要攜帶參數: //查詢所有的用戶,查詢條件:發布過標題中有first的post ``` $query = Model::with([ '關系名稱' => function ($query) use ($searchWord) { //對關聯表進行篩選,可以用where,orWhere 等 $query ->where('title', 'like', '%'.$searchWord.'%'); } ])->get(); ``` ## 2. whereHas 介紹 whereHas只會返回有結果的值,就可以有效避免with中出現空的返回值的情況 使用方法 基礎用法與with相似 ``` $query = Model::whereHas('關系名稱', function ($query) { $query->where('title', 'like', '%first%'); })->get(); ``` 但是whereHas可以嵌套 ``` $query = TeacherModel::query()->whereHas( 'merchant' , function ($query){ $query->whereHas( 'groups',function($query){ $query->where("查詢字段","目標值"); } ); } )->where('name','like','%'.$searchWord.'%')->get(['id','name']); ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看