php - Laravel 使用 Eloquent 多对多关系

标签 php laravel eloquent laravel-5.1

我正在尝试使用 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 表中获取相册名称。

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

Album.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()

最佳答案

您正在尝试对模型的 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;
    }
}

关于php - Laravel 使用 Eloquent 多对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32059809/

相关文章:

php - 基于PHP的系统中与域(国家)有关的横幅

php - 如何从数据库中获取数据的详细信息?

php - 在 Laravel 5 中使用表单请求验证时如何添加自定义验证规则

php - Laravel:查询具有今天时间戳的所有记录

php - 如何使用动态链接在 Laravel/Eloquent 中使用动态变量运行查询

php - Ardent 模型在保存后无法验证

javascript - 在提交 form2 之前从 form1 获取值到 form2 的任何可能方法

php - 如何使用 php 从日期时间字符串中分离日期和时间?

mysql - 尝试使用数据库值填充下拉列表时出现问题

mysql - 使用with()函数进行模型关系预加载的 Eloquent 查询问题