【问题标题】:Laravel many to many relationship using EloquentLaravel 使用 Eloquent 建立多对多关系
【发布时间】:2015-11-10 15:30:34
【问题描述】:

我正在尝试使用 Laravel 创建多对多关系,但我被卡住了。

这是我当前的表格模型:

专辑

album_id
name
created_at

用户图像

user_image_id
value

albumxuser_image(连接表)

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

我希望能够从 albumxuser_image 表中获取专辑名称。

这是我到目前为止所做的。

相册.php 模型

namespace App\Models;

use Illuminate\Database\Eloquent\Model;


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(我在练习,所以没用view)

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.
    }
});

AlbumxUserImage.php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;


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 Illuminate\Database\Eloquent\Collection::AlbumxUserImage()

【问题讨论】:

  • 您有AlbumxUserImage 型号吗?我们能看到吗?
  • 为什么不直接从Album 模型中获取专辑名称?为什么要先查找枢轴?

标签: php laravel eloquent laravel-5.1


【解决方案1】:

您正在尝试对 Collection 的模型而不是每个单独的模型调用 AlbumxUserImage()

AlbumxUserImage::all() 正在返回模型的Collection,您可以将其视为一个数组。您需要遍历集合并在集合中的每个模型上调用 AlbumxUserImage()

这可能暂时解决你的问题,但你似乎不明白 many-to-many relationships 在 Laravel 中是如何工作的。

你应该如何做多对多

我不知道您为什么要为数据透视表创建模型。这不是 Laravel 通常处理具有多对多关系的模型的方式。与您的表的典型多对多关系如下所示:

型号:

class Album extends Model {
    protected $table = 'album';
    protected $primaryKey = 'album_id';

    public function images() {
        return $this->belongsToMany('App\UserImage', '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('App\Album', '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;
    }
}

【讨论】:

  • 非常感谢。我在问问题之前阅读了文档,但我无法清楚地理解它。现在我想我明白了,谢谢。
猜你喜欢
  • 2019-01-21
  • 2019-06-13
  • 2019-03-05
  • 2016-10-05
  • 2017-11-03
  • 2019-03-26
  • 1970-01-01
  • 2019-08-17
相关资源
最近更新 更多