問題描述
我正在嘗試使用 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模板網!