Relation structure
class User {
public function students() {
return $this -> belongsToMany(Student::class, 'users_students');
}
}
class Students {
public function groups() {
return $this -> belongsToMany(Group::class, 'students_groups');
}
public function posts() {
return $this -> belongsToMany(Post::class, 'posts_students');
}
}
class Groups {
public function posts() {
return $this -> belongsToMany(Post::class, 'posts_groups');
}
}
I can get all User -> Posts
$news = [];
foreach (User::find($id)->students AS $students){
// get all posts by student
foreach ($students->posts()->where('public', '=', 1)->orderBy('date', 'desc')->get() AS $value)
$news[$value->id] = ['date' => $value->date, 'data' => $value];
//get student groups
foreach ($students->groups AS $groups){
foreach ($groups->posts()->where('.public', '=', 1)->orderBy('date', 'desc')->get() AS $value)
$news[$value->id] = ['date' => $value->date, 'data' => $value];
}
}
Use $value->id to get unique Posts.
Posts also can be made without relations.
Maby my structure not made for my needs.
I tride, but there is just error.
User::find($id)->students()->posts()->paginate(50);
Is there posible way to get in the same time students -> posts and students->groups->posts by paginate?