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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:01:37  来源:igfitidea点击:

Laravel 4 and Eloquent: retrieving all records and all related records

ormlaravelforeign-key-relationshipeloquent

提问by tptcat

I have two classes: Artistand Instrument. Each Artistcan play one or more Instruments. And each Instrumentcan be assigned to one or more Artists. So, I've set up the following Classes:

我有两个类:ArtistInstrument。每个人Artist可以玩一个或多个Instrument。并且每个Instrument都可以分配给一个或多个Artists。所以,我设置了以下类:

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个问题:

  1. In my view, I can output the $artistlike:

    {{ $artist->firstname }}
    

    and I can iterate through $instrumentslike:

    @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 $artistiterate over their $instruments?

  2. 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.

  3. 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.

  1. 在我看来,我可以输出$artist如下:

    {{ $artist->firstname }}
    

    我可以$instruments像这样迭代:

    @foreach ($instruments as $instrument)
        <h2>{{ $instrument->name }}</h2>
    @endforeach
    

    但是是否可以迭代$artist(我知道只有一个 - ?见#2)并且每次$artist迭代它们$instruments

  2. 在我的控制器中,我将如何获得所有艺术家以及每个艺术家的相关乐器,最终目标是在我的视图中迭代它们,如#1 中所述。

  3. 是否可以仅检索上述示例中的特定列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_instrumentsand it's also trying to retrieve columns that wouldn't exists in that table (like name).

我假设我的模型关系中有些不对劲。我也试着定义我的关系Artist.phphasMany(我认为它更有意义,说:“每一位艺术家的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