Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel使用雄辩的语言处理多对多关系_Php_Laravel_Eloquent_Laravel 5.1 - Fatal编程技术网

Php Laravel使用雄辩的语言处理多对多关系

Php Laravel使用雄辩的语言处理多对多关系,php,laravel,eloquent,laravel-5.1,Php,Laravel,Eloquent,Laravel 5.1,我试图用Laravel建立一种多对多的关系,但我被卡住了 以下是我当前的表格模型: 相册 用户图像 user_image_id value albumxuser_图像(连接表) 我希望能够从albumxuser_图像表中获取唱片集名称 这是我到目前为止所做的 Album.php模型 namespace App\Models; use Illuminate\Database\Eloquent\Model; class Album extends Model { /**

我试图用Laravel建立一种多对多的关系,但我被卡住了

以下是我当前的表格模型:

相册

用户图像

user_image_id
value
albumxuser_图像(连接表)

我希望能够从albumxuser_图像表中获取唱片集名称

这是我到目前为止所做的

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');
    }
}
php(我没有使用视图,因为我正在练习)

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

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

这可能暂时解决了您的问题,但您似乎不了解如何在Laravel中工作

你应该如何做多对多 我不知道你为什么要为透视表设计一个模型。这不是Laravel通常处理具有多对多关系的模型的方式。与表的典型多对多关系如下所示:

型号:

用法:


您有
AlbumxUserImage
型号吗?我们能看到它吗?为什么不直接从
相册
模型中获取相册名称呢?为什么要先查找枢轴?非常感谢。在我问这个问题之前,我阅读了文件,但我不能清楚地理解它。现在我想我明白了,谢谢。
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.
    }
});
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()
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;
    }
}