Multi-model Many Relationships
On Laravel 5.1; I've got a group with many users that have many reviews. I want to query all reviews of all users for a given group, paginate and list. So far I've got a users() belongsToMany relationship on the Group model, and a reviews() hasMany relationship on the User model. I feel that there must be a clean means to query and sort such a list leveraging these relationships/models, but all current attempts I've been unable to do so.
My current placeholder implementation, and functions/models mentioned above, are as follows..
User Model;
class User extends BaseModel {
public function reviews() {
return $this->hasMany('App\Models\Review', 'user_id')
->orderBy('created_at', 'desc');
}
}
Group Model;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator as Paginator;
use Illuminate\Pagination;
class Group extends BaseModel {
public function users() {
return $this->belongsToMany('App\Models\User', 'users_groups');
}
public function reviews() {
$reviews = [];
foreach($this->users()->get() as $user) {
foreach($user->reviews as $review) {
array_push($reviews, $review);
}
}
return new Paginator($reviews, count($reviews), 25);
}
}
.. this however doesn't feel efficient, results in broken pagination not rendering as expected and will need me to manually sort the array list by the desired created_at of a users review (group->users->user->completions->created_at).
Group Controller;
public function groupReviews($id) {
$group = $this->group->findById($id);
$reviews = $group->reviews();
return view('groups/group-reviews', compact('group', 'reviews'));
}
Group View;
@forelse($reviews as $review)
{{ $review->title }} - {{ $review->created_at }}
@empty
There are no reviews!<br>
@endforelse
Any advice on how to better approach this would be greatly appreciated.
Please or to participate in this conversation.