Laravel 4和Eloquent:检索所有记录和所有相关记录 [英] Laravel 4 and Eloquent: retrieving all records and all related records

查看:90
本文介绍了Laravel 4和Eloquent:检索所有记录和所有相关记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个课程:艺术家乐器。每个艺术家可以播放一个或多个乐器。并且每个乐器可以分配到一个或多个艺术家。所以,我设置了以下课程:

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

Artist.php

public function instruments() {
    return $this->belongsToMany('App\Models\Instrument');
}

Instrument.php

public function artists() {
    return $this->belongsToMany('\App\Models\Artist');
}



那么我有三个数据库表: p>


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 one artist and their associated instruments like so:

ArtistController.php

$artist = Artist::find($artist_id);
$instruments = $artist->instruments()->get();
return \View::make('artists')->with('artists', $artists)->with('instruments', $instruments);

我有3个问题:


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

{{ $artist->firstname }}

,我可以通过 $ instruments 来迭代,如:

and I can iterate through $instruments like:

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

但是可以迭代 $ artist (我知道只有一个 - 见#2),每个 $ artist 迭代他们的 $ instruments

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 all artists and for each of those their associated instruments with the end goal of iterating through them in my view as described in #1.

可以在上述<$ c $的示例中检索特定的列C> ArtistController.php ?我已经尝试过:

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

但是我收到一条错误,说 Collection :: instruments()未定义。

but I get an error saying Collection::instruments() is undefined.

我假设在我的模型关系中有一些不正确的东西。我也尝试用 Artist.php 定义我的关系,一个 hasMany (我觉得说每个艺术家都有很多乐器 ,但是这给我一个错误,因为它期待一个名为 artists_instruments 的表,它也试图检索该表中不存在的列(如

I'm assuming there's something not right in my model relationships. I've also tried defining my relationship in Artist.php with 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).

推荐答案

您的模型关系很好。

控制器:

$artists = Artist::with('instruments')->get();

return \View::make('artists')->withArtists($artists);

查看:

@foreach ($artists as $artist)

    <h1>{{ $artist->firstname }}</h1>

    @foreach ($artist->instruments as $instrument)

        <h2>{{ $instrument->name }}</h2>

    @endforeach

@endforeach

这篇关于Laravel 4和Eloquent:检索所有记录和所有相关记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆