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

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

        <legend id='1Lov6'><style id='1Lov6'><dir id='1Lov6'><q id='1Lov6'></q></dir></style></legend>

        <tfoot id='1Lov6'></tfoot>

        <small id='1Lov6'></small><noframes id='1Lov6'>

      1. Laravel 使用 Eloquent 的多對多關系

        Laravel many to many relationship using Eloquent(Laravel 使用 Eloquent 的多對多關系)
        • <small id='emOOu'></small><noframes id='emOOu'>

        • <tfoot id='emOOu'></tfoot>
          <i id='emOOu'><tr id='emOOu'><dt id='emOOu'><q id='emOOu'><span id='emOOu'><b id='emOOu'><form id='emOOu'><ins id='emOOu'></ins><ul id='emOOu'></ul><sub id='emOOu'></sub></form><legend id='emOOu'></legend><bdo id='emOOu'><pre id='emOOu'><center id='emOOu'></center></pre></bdo></b><th id='emOOu'></th></span></q></dt></tr></i><div class="5htvt5t" id='emOOu'><tfoot id='emOOu'></tfoot><dl id='emOOu'><fieldset id='emOOu'></fieldset></dl></div>
            <tbody id='emOOu'></tbody>
            <legend id='emOOu'><style id='emOOu'><dir id='emOOu'><q id='emOOu'></q></dir></style></legend>

                  <bdo id='emOOu'></bdo><ul id='emOOu'></ul>
                • 本文介紹了Laravel 使用 Eloquent 的多對多關系的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我正在嘗試使用 Laravel 創建多對多關系,但我被卡住了.

                  I'm trying to create a many to many relationship using Laravel, but I am stuck.

                  這是我當前的表格模型:

                  Here's my current table model:

                  專輯

                  album_id
                  name
                  created_at
                  

                  用戶圖片

                  user_image_id
                  value
                  

                  albumxuser_image(連接表)

                  albumxuser_image (junction table)

                  albumxuser_image_id (primary key & auto increment)
                  album_id (FK from album)
                  user_image_id (FK from user_image)
                  

                  我希望能夠從專輯 xuser_image 表中獲取專輯名稱.

                  I want to be able to get the album name from albumxuser_image table.

                  這是我到目前為止所做的.

                  Here's what I've done so far.

                  Album.php 模型

                  Album.php model

                  namespace AppModels;
                  
                  use IlluminateDatabaseEloquentModel;
                  
                  
                  class Album extends Model {
                  
                      /**
                       * The database table used by the model.
                       *
                       * @var string
                       */
                      protected $table = 'album';
                      protected $primaryKey = 'album_id';
                  
                      public function AlbumxUserImage() {
                          return $this->belongsToMany('AlbumxUserImage', 'albumxuser_image','album_id','user_image_id');
                      }
                  }
                  

                  routes.php(我沒有使用視圖,因為我正在練習)

                  routes.php (I didn't use view since I'm making a practice)

                  Route::get('all', function() {
                      $albumxuserimage = AlbumxUserImage::all();
                      foreach ($albumxuserimage->AlbumxUserImage as $getthem) {
                          echo $getthem->pivot->name; // I want to get the name column of the album table.
                      }
                  });
                  

                  相冊xUserImage.php

                  AlbumxUserImage.php

                  namespace AppModels;
                  
                  use IlluminateDatabaseEloquentModel;
                  
                  
                  class AlbumxUserImage extends Model {
                  
                      protected $table = 'albumxuser_image';
                      protected $primaryKey = 'albumxuser_image_id';
                  
                      /**
                       * The attributes that are mass assignable.
                       *
                       * @var array
                       */
                      protected $fillable = ['album_id', 'user_image_id'];
                  }
                  

                  這是我得到的錯誤

                  Call to undefined method IlluminateDatabaseEloquentCollection::AlbumxUserImage()
                  

                  推薦答案

                  您正在嘗試對 Collection 模型而不是每個模型調用 AlbumxUserImage()模型.

                  You're trying to call AlbumxUserImage() on a Collection of models instead of on each individual model.

                  AlbumxUserImage::all() 返回一個 Collection 模型,您可以將其視為一個數組.您需要遍歷集合并在集合中的每個模型上調用 AlbumxUserImage().

                  AlbumxUserImage::all() is returning a Collection of models, which you can think of as an array. You need to iterate over the collection and call AlbumxUserImage() on each model in the collection.

                  這可能暫時解決您的問題,但您似乎不明白 多對多關系在 Laravel 中有效.

                  That may solve your problem for now, but you seem to not understand how many-to-many relationships work in Laravel.

                  我不知道為什么你的數據透視表有一個模型.這不是 Laravel 通常處理多對多關系模型的方式.與您的表的典型多對多關系如下所示:

                  I don't know why you have a model for your pivot table. That is not how Laravel normally handles models with many-to-many relationships. A typical many-to-many relationship with your tables would look like this:

                  模型:

                  class Album extends Model {
                      protected $table = 'album';
                      protected $primaryKey = 'album_id';
                  
                      public function images() {
                          return $this->belongsToMany('AppUserImage', 'albumxuser_image','album_id','user_image_id');
                      }
                  }
                  
                  class UserImage extends Model {
                      protected $table = 'user_image';
                      protected $primaryKey = 'user_image_id';
                  
                      public function albums() {
                          return $this->belongsToMany('AppAlbum', 'albumxuser_image','user_image_id','album_id');
                      }
                  }
                  

                  用法:

                  // Get all album names through UserImage
                  $userImages = UserImage::all();
                  foreach ($userImages as $userImage) {
                      foreach ($userImage->albums as $album) {
                          echo $album->name;
                      }
                  }
                  
                  // Get all images through Album
                  $albums = Album::all();
                  foreach ($albums as $album) {
                      foreach ($album->images as $image) {
                          echo $image->value;
                      }
                  }
                  

                  這篇關于Laravel 使用 Eloquent 的多對多關系的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

                  相關文檔推薦

                  MySQLi prepared statement amp; foreach loop(MySQLi準備好的語句amp;foreach 循環)
                  Is mysqli_insert_id() gets record from whole server or from same user?(mysqli_insert_id() 是從整個服務器還是從同一用戶獲取記錄?)
                  PHP MySQLi doesn#39;t recognize login info(PHP MySQLi 無法識別登錄信息)
                  mysqli_select_db() expects exactly 2 parameters(mysqli_select_db() 需要 2 個參數)
                  Php mysql pdo query: fill up variable with query result(Php mysql pdo 查詢:用查詢結果填充變量)
                  MySQLI 28000/1045 Access denied for user #39;root#39;@#39;localhost#39;(MySQLI 28000/1045 用戶“root@“localhost的訪問被拒絕)
                  <legend id='Ze1qG'><style id='Ze1qG'><dir id='Ze1qG'><q id='Ze1qG'></q></dir></style></legend>

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

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

                            <tbody id='Ze1qG'></tbody>

                          1. 主站蜘蛛池模板: 污水处理设备维修_污水处理工程改造_机械格栅_过滤设备_气浮设备_刮吸泥机_污泥浓缩罐_污水处理设备_污水处理工程-北京龙泉新禹科技有限公司 | 土壤养分检测仪_肥料养分检测仪_土壤水分检测仪-山东莱恩德仪器 大型多片锯,圆木多片锯,方木多片锯,板材多片锯-祥富机械有限公司 | 医养体检包_公卫随访箱_慢病随访包_家签随访包_随访一体机-济南易享医疗科技有限公司 | 时代北利离心机,实验室离心机,医用离心机,低速离心机DT5-2,美国SKC采样泵-上海京工实业有限公司 工业电炉,台车式电炉_厂家-淄博申华工业电炉有限公司 | 伟秀电气有限公司-10kv高低压开关柜-高低压配电柜-中置柜-充气柜-欧式箱变-高压真空断路器厂家 | 生产加气砖设备厂家很多,杜甫机械加气砖设备价格公道 | AGV叉车|无人叉车|AGV智能叉车|AGV搬运车-江西丹巴赫机器人股份有限公司 | 带式过滤机厂家_价格_型号规格参数-江西核威环保科技有限公司 | 短信营销平台_短信群发平台_106短信发送平台-河南路尚 | 磁力抛光研磨机_超声波清洗机厂家_去毛刺设备-中锐达数控 | 变色龙云 - 打包app_原生app_在线制作平台_短链接_ip查询 | MTK核心板|MTK开发板|MTK模块|4G核心板|4G模块|5G核心板|5G模块|安卓核心板|安卓模块|高通核心板-深圳市新移科技有限公司 | 纸张环压仪-纸张平滑度仪-杭州纸邦自动化技术有限公司 | 合肥地磅_合肥数控切割机_安徽地磅厂家_合肥世佳电工设备有限公司 | vr安全体验馆|交通安全|工地安全|禁毒|消防|安全教育体验馆|安全体验教室-贝森德(深圳)科技 | 压缩空气检测_气体_水质找上海京工-服务专业、价格合理 | 扫地车厂家-山西洗地机-太原电动扫地车「大同朔州吕梁晋中忻州长治晋城洗地机」山西锦力环保科技有限公司 | SMN-1/SMN-A ABB抽屉开关柜触头夹紧力检测仪-SMN-B/SMN-C-上海徐吉 | 粉末冶金注射成型厂家|MIM厂家|粉末冶金齿轮|MIM零件-深圳市新泰兴精密科技 | 点焊机-缝焊机-闪光对焊机-电阻焊设备生产厂家-上海骏腾发智能设备有限公司 | 深圳天际源广告-形象堆头,企业文化墙,喷绘,门头招牌设计制作专家 | 佛山市钱丰金属不锈钢蜂窝板定制厂家|不锈钢装饰线条|不锈钢屏风| 电梯装饰板|不锈钢蜂窝板不锈钢工艺板材厂家佛山市钱丰金属制品有限公司 | 云南成人高考网| 威实软件_软件定制开发_OA_OA办公系统_OA系统_办公自动化软件 | 杭州中策电线|中策电缆|中策电线|杭州中策电缆|杭州中策电缆永通集团有限公司 | 长沙广告公司|长沙广告制作设计|长沙led灯箱招牌制作找望城湖南锦蓝广告装饰工程有限公司 | 对辊式破碎机-对辊制砂机-双辊-双齿辊破碎机-巩义市裕顺机械制造有限公司 | 螺钉式热电偶_便携式温度传感器_压簧式热电偶|无锡联泰仪表有限公司|首页 | 胜为光纤光缆_光纤跳线_单模尾纤_光纤收发器_ODF光纤配线架厂家直销_北京睿创胜为科技有限公司 - 北京睿创胜为科技有限公司 | 北京宣传片拍摄_产品宣传片拍摄_宣传片制作公司-现像传媒 | 防爆电机-高压防爆电机-ybx4电动机厂家-河南省南洋防爆电机有限公司 | 北京翻译公司-专业合同翻译-医学标书翻译收费标准-慕迪灵 | 水热合成反应釜-防爆高压消解罐-西安常仪仪器设备有限公司 | 无纺布包装机|径向缠绕包装机|缠绕膜打包机-上海晏陵智能设备有限公司 | 九爱图纸|机械CAD图纸下载交流中心| 变频器维修公司_plc维修_伺服驱动器维修_工控机维修 - 夫唯科技 变位机,焊接变位机,焊接变位器,小型变位机,小型焊接变位机-济南上弘机电设备有限公司 | 书法培训-高考书法艺考培训班-山东艺霖书法培训凭实力挺进央美 | 压力控制器,差压控制器,温度控制器,防爆压力控制器,防爆温度控制器,防爆差压控制器-常州天利智能控制股份有限公司 | 打包钢带,铁皮打包带,烤蓝打包带-高密市金和金属制品厂 | 电采暖锅炉_超低温空气源热泵_空气源热水器-鑫鲁禹电锅炉空气能热泵厂家 | 德国UST优斯特氢气检漏仪-德国舒赐乙烷检测仪-北京泽钏 |