<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之旅 廣告
                ### [](https://octobercms.com/docs/database/relations#inserting-related-models)插入相關模型 就像您[查詢關系一樣](https://octobercms.com/docs/database/relations#querying-relations),October支持使用方法或動態屬性方法定義關系。例如,也許您需要`Comment`為`Post`模型插入一個新的。您可以直接從關系中插入,而不必在上手動設置`post_id`屬性。`Comment``Comment` ### [](https://octobercms.com/docs/database/relations#inserting-method)通過關系方法插入 October提供了將新模型添加到關系的便捷方法。主要可以將模型添加到關系中或從關系中刪除。在每種情況下,關系分別是關聯的或分離的。 #### 添加方式 使用該`add`方法來關聯新關系。 ~~~ $comment = new Comment(['message' => 'A new comment.']); $post = Post::find(1); $comment = $post->comments()->add($comment); ~~~ 注意,我們沒有將`comments`關系作為動態屬性來訪問。相反,我們調用該`comments`方法來獲取關系的實例。該`add`方法將自動`post_id`為新`Comment`模型添加適當的值。 如果需要保存多個相關模型,則可以使用以下`addMany`方法: ~~~ $post = Post::find(1); $post->comments()->addMany([ new Comment(['message' => 'A new comment.']), new Comment(['message' => 'Another comment.']), ]); ~~~ #### 刪除方法 相比之下,該`remove`方法可用于取消關聯,使其成為孤立記錄。 ~~~ $post->comments()->remove($comment); ~~~ 對于多對多關系,該記錄將從關系的集合中刪除。 ~~~ $post->categories()->remove($category); ~~~ 如果是“屬于”關系,則可以使用該`dissociate`方法,該方法不需要將相關模型傳遞給它。 ~~~ $post->author()->dissociate(); ~~~ #### 添加樞軸數據 當使用多對多關系時,該`add`方法將附加的中間“樞軸”表屬性數組作為其第二個參數接受為數組。 ~~~ $user = User::find(1); $pivotData = ['expires' => $expires]; $user->roles()->add($role, $pivotData); ~~~ 該`add`方法的第二個參數還可以指定當作為字符串傳遞時,[延遲綁定](https://octobercms.com/docs/database/relations#deferred-binding)使用的會話密鑰。在這些情況下,可以將數據透視表作為第三個參數提供。 ~~~ $user->roles()->add($role, $sessionKey, $pivotData); ~~~ #### 創建方法 雖然`add`并`addMany`接受一個完整的模型實例,你也可以使用該`create`方法,接受屬性的PHP數組,創建了一個模型,并將其插入到數據庫中。 ~~~ $post = Post::find(1); $comment = $post->comments()->create([ 'message' => 'A new comment.', ]); ~~~ 在使用該`create`方法之前,請確保查看有關屬性[質量分配](https://octobercms.com/docs/database/model#mass-assignment)的文檔,因為PHP數組中的屬性受模型的“可填充”定義限制。 ### [](https://octobercms.com/docs/database/relations#inserting-dynamic-property)通過動態屬性插入 關系可以通過訪問它們的方式直接通過它們的屬性來設置。使用這種方法設置關系將覆蓋以前存在的任何關系。之后應像保存任何屬性一樣保存模型。 ~~~ $post->author = $author; $post->comments = [$comment1, $comment2]; $post->save(); ~~~ 或者,您可以使用主鍵設置關系,這在使用HTML表單時非常有用。 ~~~ // Assign to author with ID of 3 $post->author = 3; // Assign comments with IDs of 1, 2 and 3 $post->comments = [1, 2, 3]; $post->save(); ~~~ 通過將NULL值分配給屬性,可以解除關聯。 ~~~ $post->author = null; $post->comments = null; $post->save(); ~~~ 與[延遲綁定](https://octobercms.com/docs/database/relations#deferred-binding)類似,在不存在的模型上定義的關系會在內存中延遲,直到它們被保存為止。在此示例中,帖子尚不存在,因此`post_id`無法通過設置評論的屬性`$post->comments`。因此,將關聯推遲到通過調用`save`方法創建帖子之前。 ~~~ $comment = Comment::find(1); $post = new Post; $post->comments = [$comment]; $post->save(); ~~~ ### [](https://octobercms.com/docs/database/relations#inserting-many-to-many-relations)多對多關系 #### 裝卸 在使用多對多關系時,模型提供了一些其他的輔助方法,使使用相關模型更加方便。例如,讓我們假設一個用戶可以有許多角色,而一個角色可以有許多用戶。要通過在連接模型的中間表中插入一條記錄來將角色附加給用戶,請使用以下`attach`方法: ~~~ $user = User::find(1); $user->roles()->attach($roleId); ~~~ 將關系附加到模型時,您還可以傳遞要插入到中間表中的其他數據數組: ~~~ $user->roles()->attach($roleId, ['expires' => $expires]); ~~~ 當然,有時可能有必要從用戶中刪除角色。若要刪除多對多關系記錄,請使用`detach`方法。該`detach`方法將從中間表中刪除適當的記錄;但是,兩個模型都將保留在數據庫中: ~~~ // Detach a single role from the user... $user->roles()->detach($roleId); // Detach all roles from the user... $user->roles()->detach(); ~~~ 為了方便起見,`attach`并且`detach`還接受ID作為輸入的數組: ~~~ $user = User::find(1); $user->roles()->detach([1, 2, 3]); $user->roles()->attach([1 => ['expires' => $expires], 2, 3]); ~~~ #### 同步方便 您也可以使用該`sync`方法構造多對多關聯。該`sync`方法接受ID數組以放置在中間表上。給定數組中沒有的任何ID將從中間表中刪除。因此,完成此操作后,中間表中將僅存在數組中的ID: ~~~ $user->roles()->sync([1, 2, 3]); ~~~ 您還可以傳遞帶有ID的其他中間表值: ~~~ $user->roles()->sync([1 => ['expires' => true], 2, 3]); ~~~ ### [](https://octobercms.com/docs/database/relations#touching-parent-timestamps)感動的父母時間戳 當一個模型`belongsTo`或`belongsToMany`另一個模型(例如`Comment`屬于a)時`Post`,在更新子模型時更新父時間戳有時會很有幫助。例如,當`Comment`模型更新時,您可能想要自動“觸摸”`updated_at`所有者的時間戳`Post`。只需`touches`向子模型添加一個包含關系名稱的屬性: ~~~ class Comment extends Model { /** * All of the relationships to be touched. */ protected $touches = ['post']; /** * Relations */ public $belongsTo = [ 'post' => ['Acme\Blog\Models\Post'] ]; } ~~~ 現在,當您更新時`Comment`,所有者`Post`將同時`updated_at`更新其列: ~~~ $comment = Comment::find(1); $comment->text = 'Edit to this comment!'; $comment->save(); ~~~
                  <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>

                              哎呀哎呀视频在线观看