我有一个问题,我需要获取所有的图片(路径),我的画廊桌子,拥有一个博物馆和用户谁拥有博物馆。我得到了图片的路径,但这些与拥有博物馆的user_id无关。
因此,简单地说:
每个用户都拥有一个博物馆,一个博物馆有一个有多个图像的画廊(图像url的路径)。
可能表结构
我的画廊模型:
<?php
class Gallery extends \Eloquent {
protected $fillable = [];
public function museums() {
//return $this->belongsToMany('Museums', 'id');
return $this->belongsTo('Gallery', 'museum_id');
}
}我的博物馆模型
<?php
class Museum extends Eloquent {
protected $fillable = ['user_id', 'title', 'description'];
public function user()
{
return $this->belongsTo('User');
}
public function gallery()
{
//return $this->belongsToMany('Gallery', 'museum_id');
return $this->belongsToMany('Gallery');
}
}我的用户模型
public function museums()
{
return $this->hasMany('Museum');
}还有我的MuseumController
public function show($id)
{
//
//$museum = Museum::where('id', '=', $id)->first();
//return View::make('museums.detail', compact('museum'));
$museum = Museum::findOrFail($id);
$gallery = Gallery::with('museums')->get();
//$museum = Museum::with('gallery')->get();
return View::make('museums.detail', compact('museum', 'gallery'));
}在我看来
@foreach ($gallery as $image)
<img src="{{ $image->path }}" />
@endforeach发布于 2014-08-09 16:28:21
你可以试试这个:
// In User model
public function museum()
{
return $this->hasOne('Museum');
}
// In Museum model
public function owner()
{
return $this->belongsTo('User');
}
// In Museum model
public function galleries()
{
return $this->hasMany('Gallery');
}
// In Gallery model
public function museum()
{
return $this->belongsTo('Museum');
}然后在控制器中:
$museums = Museum::with('galleries', 'owner')->get();
return View::make('museums.detail', compact('museums'));在你看来:
@foreach ($museums as $museum)
{{ $museum->title }}
// To get the user id from here
{{ $museum->owner->id }}
// Loop all images in this museum
@foreach($museum->galleries as $image)
<img src="{{ $image->path }}" />
// To get the user id from here
{{ $image->museum->owner->id }}
@endforeach
@endforeachhttps://stackoverflow.com/questions/25219798
复制相似问题