Skip to content
Advertisement

Running a for loop iteration on Laravel Eloquent

I have an model $club which is related to another model $events.

I query the database for clubs using this

JavaScript

Then for the events, I am trying to copy all the events associated to ALL clubs by doing

JavaScript

The problem is when I echo events, I only see the events for just one club rather than all the clubs, its almost like my loop isn’t iterating.

Advertisement

Answer

For best pratices and performance, use the Laravel relationships.

In your Club Model includes hasMany function like this:

JavaScript

Then your controller..

JavaScript

After this, you will have events in your Collect $clubs

If you want only the events, could get by this

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement