Hi Friend You Can User Relation or Query as below
public function getStudentBook() //get the student book
{
$user = auth()->user();
if($user->role == 'student') { //assuming role column has student or teacher value
//assuming user_books as pivot table for users and book table
$books = Book::join('user_books', 'user_books.student_id', '=', 'users.id')
->where('user_books.student_id', '=', $user->id)
->groupBy(user_books.book_id)
->get();
}
}
public function getStudenstBook() //get the students book of teacher
{
$user = auth()->user();
if($user->role == 'teacher') { //assuming role column has student or teacher value
//assuming student_teacher table as as pivot table for teacher student table
$student_ids = DB::table('student_teacher')->where('teacher_id' , '=', $user->id)
->grouBy('student_teacher.student_ids')->pluck('student_ids')->toArray();
//assuming user_books as pivot table for users and book table
$books = Book::join('user_books', 'user_books.student_id', '=', 'users.id')
->whereIn('user_books.student_id', $student_ids)
->groupBy(user_books.book_id)
->get();
}
}
If Any Doubt Feel Free to drop a comment. Thank you, Have a nice day