久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

      <tfoot id='XJt4l'></tfoot>

        <small id='XJt4l'></small><noframes id='XJt4l'>

        • <bdo id='XJt4l'></bdo><ul id='XJt4l'></ul>
        <legend id='XJt4l'><style id='XJt4l'><dir id='XJt4l'><q id='XJt4l'></q></dir></style></legend>

      1. <i id='XJt4l'><tr id='XJt4l'><dt id='XJt4l'><q id='XJt4l'><span id='XJt4l'><b id='XJt4l'><form id='XJt4l'><ins id='XJt4l'></ins><ul id='XJt4l'></ul><sub id='XJt4l'></sub></form><legend id='XJt4l'></legend><bdo id='XJt4l'><pre id='XJt4l'><center id='XJt4l'></center></pre></bdo></b><th id='XJt4l'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='XJt4l'><tfoot id='XJt4l'></tfoot><dl id='XJt4l'><fieldset id='XJt4l'></fieldset></dl></div>

        將數據插入到 Laravel 中的數據透視表

        Insert data to a pivot table in laravel(將數據插入到 Laravel 中的數據透視表)
        1. <tfoot id='7ZoSd'></tfoot><legend id='7ZoSd'><style id='7ZoSd'><dir id='7ZoSd'><q id='7ZoSd'></q></dir></style></legend>

            <small id='7ZoSd'></small><noframes id='7ZoSd'>

                <tbody id='7ZoSd'></tbody>

                  <bdo id='7ZoSd'></bdo><ul id='7ZoSd'></ul>
                • <i id='7ZoSd'><tr id='7ZoSd'><dt id='7ZoSd'><q id='7ZoSd'><span id='7ZoSd'><b id='7ZoSd'><form id='7ZoSd'><ins id='7ZoSd'></ins><ul id='7ZoSd'></ul><sub id='7ZoSd'></sub></form><legend id='7ZoSd'></legend><bdo id='7ZoSd'><pre id='7ZoSd'><center id='7ZoSd'></center></pre></bdo></b><th id='7ZoSd'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='7ZoSd'><tfoot id='7ZoSd'></tfoot><dl id='7ZoSd'><fieldset id='7ZoSd'></fieldset></dl></div>
                  本文介紹了將數據插入到 Laravel 中的數據透視表的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我有 3 個表:poststagspost_tag.

                  I have 3 tables: posts, tags, post_tag.

                  每個Post 都有很多標簽,所以我對它們使用 hasMany 方法.但是,當我在下拉列表中選擇例如 3 個標簽時,我無法將它們添加到 post_tag,因此我無法選擇和顯示每個帖子的標簽.

                  Each Post has many tags so I use hasMany method for them. But when I choose for example 3 tags in my dropdown list, I can't add them to post_tag and as the result I can't select and show each post's tags.

                  我的發布模型:

                   class Post extends Eloquent{
                   public function tag()
                           {
                             return  $this->hasMany('Tag');
                           }
                      }
                  

                  我的標簽模型:

                  class Tag extends Eloquent{
                   public function post()
                           {
                             return  $this->belongsToMany('Post');
                           }
                  

                  }

                  還有我的postController:

                  class postController extends BaseController{
                  
                  public function addPost(){
                  
                      $post=new Post;
                  
                      $post_title=Input::get('post_title');
                      $post_content=Input::get('post_content');
                      $tag_id=Input::get('tag');
                  
                      $post->tag()->sync($tag_id);
                      $post->save();
                  

                  我希望將此 post_id 保存到 post_tag 表及其標簽 ID,但它不起作用.感謝您抽出寶貴時間.

                  I expect to save this post_id save to post_tag table with its tag ids, but it doesn't work. Thanks for your time.

                  推薦答案

                  您的基本想法是正確的,但是您的代碼存在一些問題.有些正在阻止它工作,有些只是常規問題.

                  You have a basic idea right, but there are a few issues with your code. Some are stopping it from working and some are just conventional issues.

                  首先,這是一個 belongsTomany 關系(你有一個數據透視表)所以你必須將關系的兩邊定義為 belongsToMany(即使 hasMany 是您考慮其中一側或兩側的方式).這是因為 Laravel 期望具有兩種不同關系類型的特定數據庫結構.

                  First off, this is a belongsTomany relationship (you have a pivot table) so you must define both sides of the relationship as belongsToMany (even if hasMany is the way you think about one or both of the side of it). This is because Laravel expects a certain database structure with the two different relationship types.

                  另一個問題(您自己發現的)是您在實際保存帖子之前將標簽添加到關系中(通過 ->tag()->sync(). 你必須先保存帖子(這樣 Laravel 才知道要為 post_id 添加到數據透視表中的 ID)然后添加關系.如果你擔心標簽部分失敗然后有不一致的數據庫你應該使用事務.

                  Another issue (that you found yourself) is that you are adding the tags to the relation (via ->tag()->sync() before you've actually saved the post. You must first save the post (so that laravel knows what ID to add to the pivot table for post_id) and then add the relations. If you are worried about the tags part failing and then having an inconsistent database you should use transactions.

                  最后,您遇到的約定"錯誤是多對多關系,根據定義,涉及結果集合.因此,tagpost 應該分別是 tagsposts.

                  Finally, the 'convention' errors you have is that a belongs-to-many relationship, by definition, involves collections of results. As such, tag and post shoudl be tags and posts respectively.

                  這是我重寫的代碼版本:

                  So here's my rewritten version of your code:

                  class Post extends Eloquent
                  {
                      public function tags()
                      {
                          return $this->belongsToMany('Tag');
                      }
                  }
                  
                  class Tag extends Eloquent
                  {
                      public function posts()
                      {
                          return $this->belongsToMany('Post');
                      }
                  }
                  
                  class PostController extends BaseController
                  {
                      public function addPost()
                      {
                          // assume it won't work
                          $success = false;
                  
                          DB::beginTransaction();
                  
                          try {
                              $post = new Post;
                  
                              // maybe some validation here...
                  
                              $post->title = Input::get('post_title');
                              $post->content = Input::get('post_content');
                  
                              if ($post->save()) {
                                  $tag_ids = Input::get('tags');
                                  $post->tags()->sync($tag_ids);
                                  $success = true;
                              }
                          } catch (Exception $e) {
                              // maybe log this exception, but basically it's just here so we can rollback if we get a surprise
                          }
                  
                          if ($success) {
                              DB::commit();
                              return Redirect::back()->withSuccessMessage('Post saved');
                          } else {
                              DB::rollback();
                              return Redirect::back()->withErrorMessage('Something went wrong');
                          }
                      }
                  }
                  

                  現在很多控制器代碼都以事務為中心——如果你不太關心它,那么你可以刪除它.此外,還有幾種方法可以完成這些事務——我采用了一種并不理想但用最少的代碼就可以說明問題的方法.

                  Now a lot of that controller code centres around the transaction stuff - if you don't care too much about that then you're all good to remove it. Also there are several ways to do that transaction stuff - I've gone with one that's not ideal but gets the point across in a minimal amount of code.

                  這篇關于將數據插入到 Laravel 中的數據透視表的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Deadlock exception code for PHP, MySQL PDOException?(PHP、MySQL PDOException 的死鎖異常代碼?)
                  PHP PDO MySQL scrollable cursor doesn#39;t work(PHP PDO MySQL 可滾動游標不起作用)
                  PHP PDO ODBC connection(PHP PDO ODBC 連接)
                  Using PDO::FETCH_CLASS with Magic Methods(使用 PDO::FETCH_CLASS 和魔術方法)
                  php pdo get only one value from mysql; value that equals to variable(php pdo 只從 mysql 獲取一個值;等于變量的值)
                  MSSQL PDO could not find driver(MSSQL PDO 找不到驅動程序)

                  • <bdo id='x5Ser'></bdo><ul id='x5Ser'></ul>

                      <small id='x5Ser'></small><noframes id='x5Ser'>

                      <legend id='x5Ser'><style id='x5Ser'><dir id='x5Ser'><q id='x5Ser'></q></dir></style></legend>

                      1. <i id='x5Ser'><tr id='x5Ser'><dt id='x5Ser'><q id='x5Ser'><span id='x5Ser'><b id='x5Ser'><form id='x5Ser'><ins id='x5Ser'></ins><ul id='x5Ser'></ul><sub id='x5Ser'></sub></form><legend id='x5Ser'></legend><bdo id='x5Ser'><pre id='x5Ser'><center id='x5Ser'></center></pre></bdo></b><th id='x5Ser'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='x5Ser'><tfoot id='x5Ser'></tfoot><dl id='x5Ser'><fieldset id='x5Ser'></fieldset></dl></div>

                        <tfoot id='x5Ser'></tfoot>
                          <tbody id='x5Ser'></tbody>
                            主站蜘蛛池模板: 91视视频在线观看入口直接观看 | 91免费在线 | 噜久寡妇噜噜久久寡妇 | 午夜免费网 | 成人在线中文字幕 | 国产免费福利小视频 | 久久久久国产一区二区三区 | 欧洲精品在线观看 | 久久久综合色 | 国产成人精品一区二区三区在线 | 99久久免费精品 | 亚洲精品久久久久久一区二区 | 欧美乱淫视频 | 国产精品久久久久久亚洲调教 | 在线视频 欧美日韩 | 欧美国产91| 美女视频黄色的 | 成人在线小视频 | 亚洲三区在线 | 欧美日本高清 | 国产精品久久久精品 | 欧美成人精品一区二区男人看 | 国产精品久久影院 | 亚洲精品久久久久久久久久久 | 亚洲天堂一区 | 不卡的av一区 | 97国产精品 | 精品无码久久久久久国产 | 91精品国产色综合久久 | 成av人电影在线 | 日韩一区二区三区在线观看视频 | 国产精品久久久久久久久久免费看 | 久久国产精品一区二区 | 黑人粗黑大躁护士 | 成人午夜网 | 亚洲97| av国产精品 | 国产综合在线视频 | 精品久久久久一区二区国产 | 一区二区在线 | 国产在线中文 |