Laravel 4 和 Eloquent:检索所有记录和所有相关记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21735011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 4 and Eloquent: retrieving all records and all related records
提问by tptcat
I have two classes: Artist
and Instrument
. Each Artist
can play one or more Instrument
s. And each Instrument
can be assigned to one or more Artist
s. So, I've set up the following Classes:
我有两个类:Artist
和Instrument
。每个人Artist
可以玩一个或多个Instrument
。并且每个Instrument
都可以分配给一个或多个Artist
s。所以,我设置了以下类:
Artist.php
艺术家.php
public function instruments() {
return $this->belongsToMany('App\Models\Instrument');
}
Instrument.php
仪器.php
public function artists() {
return $this->belongsToMany('\App\Models\Artist');
}
Then I have three database tables:
然后我有三个数据库表:
artists: id, firstname, lastname, (timestamps)
instruments: id, name
artist_instrument: id, artist_id, instrument_id
I'm able to successfully retrieve a oneartist and their associated instruments like so:
我能够成功检索一位艺术家及其相关乐器,如下所示:
ArtistController.php
艺术家控制器.php
$artist = Artist::find($artist_id);
$instruments = $artist->instruments()->get();
return \View::make('artists')->with('artists', $artists)->with('instruments', $instruments);
I have 3 questions:
我有3个问题:
In my view, I can output the
$artist
like:{{ $artist->firstname }}
and I can iterate through
$instruments
like:@foreach ($instruments as $instrument) <h2>{{ $instrument->name }}</h2> @endforeach
but is it possible to iterate over
$artist
(I know there's only one —?see #2) and for each$artist
iterate over their$instruments
?In my controller, how would I get allartists and for each of those their associated instruments with the end goal of iterating through them in my view as described in #1.
Is it possible to only retrieve specific columns in the above example of
ArtistController.php
? I've tried this:$artist = Artist::where('id', $artist_id)->get('firstname'); $instruments = $artist->instruments()->get(); return \View::make('artists')->with('artists', $artists)->with('instruments', $instruments);
but I get an error saying
Collection::instruments()
is undefined.
在我看来,我可以输出
$artist
如下:{{ $artist->firstname }}
我可以
$instruments
像这样迭代:@foreach ($instruments as $instrument) <h2>{{ $instrument->name }}</h2> @endforeach
但是是否可以迭代
$artist
(我知道只有一个 - ?见#2)并且每次$artist
迭代它们$instruments
?在我的控制器中,我将如何获得所有艺术家以及每个艺术家的相关乐器,最终目标是在我的视图中迭代它们,如#1 中所述。
是否可以仅检索上述示例中的特定列
ArtistController.php
?我试过这个:$artist = Artist::where('id', $artist_id)->get('firstname'); $instruments = $artist->instruments()->get(); return \View::make('artists')->with('artists', $artists)->with('instruments', $instruments);
但我收到一条错误消息,说
Collection::instruments()
未定义。
I'm assuming there's something not right in my model relationships. I've also tried defining my relationship in Artist.phpwith a hasMany
(I think it makes more sense to say "Each Artist hasMany Instruments", but that gives me an error because it's expecting a table named artists_instruments
and it's also trying to retrieve columns that wouldn't exists in that table (like name
).
我假设我的模型关系中有些不对劲。我也试着定义我的关系Artist.php与hasMany
(我认为它更有意义,说:“每一位艺术家的hasMany仪器”,但给我一个错误,因为它是期待一个表命名artists_instruments
,它也试图找回那列该表中不存在(如name
)。
回答by Joseph Silber
Your model relationships are fine.
你的模型关系很好。
Controller:
控制器:
$artists = Artist::with('instruments')->get();
return \View::make('artists')->withArtists($artists);
View:
看法:
@foreach ($artists as $artist)
<h1>{{ $artist->firstname }}</h1>
@foreach ($artist->instruments as $instrument)
<h2>{{ $instrument->name }}</h2>
@endforeach
@endforeach