So, I have three models - Events, Groups and Individuals. I'm trying to create an API response that provides a list of groups AND the individuals in each group. I've been able to create a response that provides a list of the groups, but I'm not sure how to also PROPERLY include the individuals of each group.
I thought about just looping through each from the first response and making another query for each, but that seemed like overkill. So, the question is how can I include the Individuals for each Group within the response as well.
I have all three eloquent models defined as such:
Events:
public function groups()
{
return $this->hasMany('App\Group');
}
Groups:
public function event()
{
return $this->belongsTo('App\Event');
}
public function individuals()
{
return $this->hasMany('App\Individual');
}
Individuals:
public function group()
{
return $this->belongsTo('App\Group');
}
Then I have my route:
Route::get('/events/{id}/groups', 'EventsController@groups');
Then that controller has the function to return the list of groups:
public function groups($eventId)
{
$event= Event::find($eventId);
$groups= $event->groups()->paginate();
return $groups;
}
Any help would be greatly appreciated.
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2iTRgdm
via IFTTT
Aucun commentaire:
Enregistrer un commentaire