pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

      <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="dthxll7" 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="flfnlx5" 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="f5t5vf7" id='x5Ser'><tfoot id='x5Ser'></tfoot><dl id='x5Ser'><fieldset id='x5Ser'></fieldset></dl></div>

                        <tfoot id='x5Ser'></tfoot>
                          <tbody id='x5Ser'></tbody>
                            主站蜘蛛池模板: 新能源汽车电机定转子合装机 - 电机维修设备 - 睿望达 | 吨袋包装机|吨包秤|吨包机|集装袋包装机-烟台华恩科技 | 重庆LED显示屏_显示屏安装公司_重庆LED显示屏批发-彩光科技公司 重庆钣金加工厂家首页-专业定做监控电视墙_操作台 | 壹车网 | 第一时间提供新车_资讯_报价_图片_排行! | 车间除尘设备,VOCs废气处理,工业涂装流水线,伸缩式喷漆房,自动喷砂房,沸石转轮浓缩吸附,机器人喷粉线-山东创杰智慧 | 焊锡丝|焊锡条|无铅锡条|无铅锡丝|无铅焊锡线|低温锡膏-深圳市川崎锡业科技有限公司 | 有源电力滤波装置-电力有源滤波器-低压穿排电流互感器|安科瑞 | 高柔性拖链电缆-聚氨酯卷筒电缆-柔性屏蔽电缆厂家-玖泰电缆 | 福州仿石漆加盟_福建仿石漆厂家-外墙仿石漆加盟推荐铁壁金钢(福建)新材料科技有限公司有保障 | 聚合氯化铝厂家-聚合氯化铝铁价格-河南洁康环保科技 | 茶叶百科网-茶叶知识与茶文化探讨分享平台 | 食品无尘净化车间,食品罐装净化车间,净化车间配套风淋室-青岛旭恒洁净技术有限公司 | 昆明化妆培训-纹绣美甲-美容美牙培训-昆明博澜培训学校 | 掺铥光纤放大器-C/L波段光纤放大器-小信号光纤放大器-合肥脉锐光电技术有限公司 | 膏剂灌装旋盖机-眼药水灌装生产线-西林瓶粉剂分装机-南通博琅机械科技 | 浙江工业冷却塔-菱电冷却塔厂家 - 浙江菱电冷却设备有限公司 | 无缝方管|无缝矩形管|无缝方矩管|无锡方管厂家 | 经济师考试_2025中级经济师报名时间_报名入口_考试时间_华课网校经济师培训网站 | 一体化净水器_一体化净水设备_一体化水处理设备-江苏旭浩鑫环保科技有限公司 | 贝壳粉涂料-内墙腻子-外墙腻子-山东巨野七彩贝壳漆业中心 | 搪瓷反应釜厂家,淄博搪瓷反应釜-淄博卓耀 | 东莞猎头公司_深圳猎头公司_广州猎头公司-广东万诚猎头提供企业中高端人才招聘服务 | 北京网站建设首页,做网站选【优站网】,专注北京网站建设,北京网站推广,天津网站建设,天津网站推广,小程序,手机APP的开发。 | 赛默飞Thermo veritiproPCR仪|ProFlex3 x 32PCR系统|Countess3细胞计数仪|371|3111二氧化碳培养箱|Mirco17R|Mirco21R离心机|仟诺生物 | 高压分散机(高压细胞破碎仪)百科-北京天恩瀚拓 | 大型多片锯,圆木多片锯,方木多片锯,板材多片锯-祥富机械有限公司 | 气体检测仪-氢气检测仪-可燃气体传感器-恶臭电子鼻-深国安电子 | 路面机械厂家 | 不锈钢管件(不锈钢弯头,不锈钢三通,不锈钢大小头),不锈钢法兰「厂家」-浙江志通管阀 | 冷水机-冰水机-冷冻机-冷风机-本森智能装备(深圳)有限公司 | 电竞学校_电子竞技培训学校学院-梦竞未来电竞学校官网 | 焊管生产线_焊管机组_轧辊模具_焊管设备_焊管设备厂家_石家庄翔昱机械 | 制氮设备-变压吸附制氮设备-制氧设备-杭州聚贤气体设备制造有限公司 | 泰来华顿液氮罐,美国MVE液氮罐,自增压液氮罐,定制液氮生物容器,进口杜瓦瓶-上海京灿精密机械有限公司 | 杭州网络公司_百度SEO优化-外贸网络推广_抖音小程序开发-杭州乐软科技有限公司 | 密集柜_档案密集柜_智能密集架_密集柜厂家_密集架价格-智英伟业 密集架-密集柜厂家-智能档案密集架-自动选层柜订做-河北风顺金属制品有限公司 | 河南膏药贴牌-膏药代加工-膏药oem厂家-洛阳今世康医药科技有限公司 | 水压力传感器_数字压力传感器|佛山一众传感仪器有限公司|首页 | 软文推广发布平台_新闻稿件自助发布_媒体邀约-澜媒宝 | 伺服电机维修、驱动器维修「安川|三菱|松下」伺服维修公司-深圳华创益 | 金属管浮子流量计_金属转子流量计厂家-淮安润中仪表科技有限公司 |