<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ## 附加一個關聯模型 您常常會需要加入新的關聯模型。例如新增一個 comment 到 post 。除了手動設定模型的 post_id 外鍵,也可以從上層的 Post 模型新增關聯的 comment : ~~~ $comment = new Comment(['message' => 'A new comment.']); $post = Post::find(1); $comment = $post->comments()->save($comment); ~~~ 上面的例子里,新增的 comment 模型中 post_id 字段會被自動設定。 如果想要同時新增很多關聯模型: ~~~ $comments = [ new Comment(['message' => 'A new comment.']), new Comment(['message' => 'Another comment.']), new Comment(['message' => 'The latest comment.']) ]; $post = Post::find(1); $post->comments()->saveMany($comments); ~~~ ## 從屬關聯模型 ( Belongs To ) 要更新 belongsTo 關聯時,可以使用 associate 方法。這個方法會設定子模型的外鍵: ~~~ $account = Account::find(10); $user->account()->associate($account); $user->save(); ~~~ ## 新增多對多關聯模型 ( Many To Many ) 您也可以新增多對多的關聯模型。讓我們繼續使用 User 和 Role 模型作為例子。我們可以使用 attach 方法簡單地把 roles 附加給一個 user: #### 附加多對多模型 ~~~ $user = User::find(1); $user->roles()->attach(1); ~~~ 也可以傳入要存在樞紐表中的屬性數組: ~~~ $user->roles()->attach(1, ['expires' => $expires]); ~~~ 當然,有 attach 方法就會有相反的 detach 方法: ~~~ $user->roles()->detach(1); ~~~ attach 和 detach 都可以接受ID數組作為參數: ~~~ $user = User::find(1); $user->roles()->detach([1, 2, 3]); $user->roles()->attach([1 => ['attribute1' => 'value1'], 2, 3]); ~~~ #### 使用 Sync 方法同時附加一個以上多對多關聯 您也可以使用 sync 方法附加關聯模型。 sync 方法會把根據 ID 數組把關聯存到樞紐表。附加完關聯后,樞紐表里的模型只會關聯到 ID 數組里的 id : ~~~ $user->roles()->sync([1, 2, 3]); ~~~ #### Sync 時在樞紐表加入額外數據 也可以在把每個 ID 加入樞紐表時,加入其他字段的數據: ~~~ $user->roles()->sync([1 => ['expires' => true]]); ~~~ 有時您可能想要使用一個命令,在建立新模型數據的同時附加關聯。可以使用 save 方法達成目的: ~~~ $role = new Role(['name' => 'Editor']); User::find(1)->roles()->save($role); ~~~ 上面的例子里,新的 Role 模型對象會在儲存的同時關聯到 user 模型。也可以傳入屬性數組把數據加到關聯數據庫表: ~~~ User::find(1)->roles()->save($role, ['expires' => $expires]); ~~~
                  <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>

                              哎呀哎呀视频在线观看