3

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


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

I have 3 questions:

  1. 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?

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

  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.

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

tptcat
  • 3,894
  • 2
  • 32
  • 51
  • 1
    Try including your relationship key for Artist in your ->get('firstname'). ( I'm assuming it would be 'id' like this: ->get('id', 'firstname') ) – Patrick Feb 12 '14 at 17:31

1 Answers1

11

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
Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
  • 2
    I just stumbled upon the answer as you posted and this is exactly what I did. Thank you! – tptcat Feb 12 '14 at 17:34
  • One more thing (and I can post this as a separate question if it makes sense) how would I specify the columns to retrieve if I didn't want to retrieve all of them? I've tried using `select()` in various places, but nothing seems to work. – tptcat Feb 12 '14 at 18:18
  • @tptcat - that should definitely be a separate question. – Joseph Silber Feb 12 '14 at 18:58
  • The followup question is here: http://stackoverflow.com/questions/21737547/laravel-and-eloquent-specifying-columns-in-when-retrieving-related-items – tptcat Feb 12 '14 at 19:20