0

I currently have a site where I am grabbing songs from a database, looping through and displaying them on the page. I now need to add an option to preview the 8bar sample of this song, I am struggling with the idea of putting a foreach inside of a foreach. I currently have the following displaying on my web page:

The markup my loops are producing

I would like to get the beat into the song container however I have a loop pushing out the song container like so:

@foreach($tracks as $track)
            <div class="song-container">
                <h2>{{$track->Title}}</h2>
                <button><a href="/mode-select/fun/freestyle/beatselect/{{$track->id}}?track={{$track->id}}">Select</a></button>
            </div>
@endforeach

and just above that I have my beat foreach:

@foreach($beats as $beat)
<a href="{{$beat->URL}}"><button class="beat-preview">Preview beat</button></a>
@endforeach

Am I missing something painfully obvious? I just need to get the anchor produced by the beat foreach into the song container div.

For clarification: I have a track table with an ID, a title etc and then the Beat table which has a relation to the Track ID, the Beat table has TrackVariantIds (Which are just 8/16/24 bar URLs basically).

Update:

I sorted my Eloquent models and can now run the following foreach:

                @foreach($tracks as $track)
            <div class="song-container">
                <h2>{{$track->Title}}</h2>
                @foreach($track->beats as $beat)
                    {{$beat->URL}}
                @endforeach
                    <a href="{{$track}}"><button class="beat-preview">Preview beat</button></a>
                <button><a href="/mode-select/fun/freestyle/beatselect/{{$track->id}}?track={{$track->id}}">Select</a></button>
            </div>
            @endforeach

    //In my Track Model
    public function beats() {
    return $this->hasMany(\app\Beat::class, 'TrackID', 'id');
}

This returns all three of the beats when actually I want to tell the above foreach to take only ones with the TrackVariantID of 1. Any ideas? :)

Thanks in advance for any help!

5
  • Where the relationship is concerned, one track has many 'beats'? If yes, then you are perfectly fine with placing the beat foreach in the track's foreach, as long as you setup their relationships properly.
    – Mysteryos
    Feb 12, 2016 at 10:55
  • Hey @Mysteryos - One track has many beats yeah, however I only need the 8bar variant on this page (as declared by my query anyway, so I am only getting the 8bar ones) I'm just unsure on how to do my loops
    – ExohJosh
    Feb 12, 2016 at 10:57
  • Each beat preview must have their own URL. Hence, your foreach beat loop should start as such: @foreach($track->beats as $beat). Else all tracks will have the same beat preview.
    – Mysteryos
    Feb 12, 2016 at 10:59
  • 1
    Maybe you need use your beats foreach in tracks foreach and change your beats foreach like foreach($track->getBeats() as $beat). Feb 12, 2016 at 11:00
  • If the beat is in the is in the track, you should have something live tracks->beats and you should be able to work from there.
    – C Cislariu
    Feb 12, 2016 at 11:00

2 Answers 2

0

If I understood you correctly, you need something like this:

# view file
@foreach($tracks as $oneTrack)
    // ...
    @foreach($oneTrack->samples as $oneSample)
        // ...
    @endforeach
@endforeach


# Model file
class Track extends Base
{

    // define relationshoip
    public function beats() {
        return $this->hasMany('Beat');
    }

    public function samples()
    {
        return $this->with(
            ['beats'=>function($query) {
                // first 8 samples
                return $query->limit(8);
            }];
    );
}

Here is not a problem in a view, blade or controller, I guess. It is an issue of data organisation (DB and Model) at first.

3
  • I think you may be right about the Model, I actually hadn't set up the relationship embarassingly enough. I will try to apply what's done here, I wouldn't need the first 8 samples but rather All of the beats that have the TrackVariantID of 1 :) - Thanks for your help!
    – ExohJosh
    Feb 12, 2016 at 11:26
  • I am glad, if it helped:-) But I have to say, that most important step is the relationship (the first method from my example). And then you may use it in views. And if you need, you might use any sql condition in the second method from my example.
    – Juljan
    Feb 12, 2016 at 11:39
  • I have tinkered with the example you sent me and am getting an error: Undefined property: Illuminate\Database\Eloquent\Collection::$getPreviews (View: songselect.blade.php) Is this because of my Eloquent model relationships?
    – ExohJosh
    Feb 12, 2016 at 12:27
0

Undefined property: Illuminate\Database\Eloquent\Collection::$getPreviews

As the error says, you're trying to get a relationship on a collection, the relationship exists on the objects (records) in that collection. It seems you have one to many relationship in DB. So:

//$tracks->beats - is incorrect.
$beats->first()->beats - should be correct.

# and in views:
foreach ($tracks ad $oneTrack):
    $beats = $oneTrack->beats; // is correct.
    //$beats->id // is incorrect
    $id = $beats->first()->id; // correct
    foreach ($beats ad $oneBeat) {
        $bid = $oneBeat; // correct
    }
endforeach;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.