• <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 id='emOOu'><tfoot id='emOOu'></tfoot><dl id='emOOu'><fieldset id='emOOu'></fieldset></dl></div>
      <legend id='emOOu'><style id='emOOu'><dir id='emOOu'><q id='emOOu'></q></dir></style></legend>

          <bdo id='emOOu'></bdo><ul id='emOOu'></ul>
      1. Laravel 使用 Eloquent 的多对多关系

        时间:2023-09-23
            <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 id='1Lov6'><tfoot id='1Lov6'></tfoot><dl id='1Lov6'><fieldset id='1Lov6'></fieldset></dl></div>
              <bdo id='1Lov6'></bdo><ul id='1Lov6'></ul>

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

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

                • 本文介绍了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模板网!

                  上一篇:如何禁用 Laravel eloquent Auto Increment? 下一篇:Laravel 过滤所有列中的值

                  相关文章

                  最新文章

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