2

I have two user types: Teacher and Student

For students the route is:

GET /me/books

the students should only have their books in response.

For teachers the route is:

GET /books

and teachers should have all the students' books in response.

I have one model: Book and one controller: BookController. What is the best approach to writing a BookController@index method in this situation? Using a switch case for user types inside the index method doesn't sound like a best practice.

Egemenk
  • 307
  • 3
  • 9
  • 2
    My personal opinion is that the time/space you save by handling two resources with the same controller is not worth the headache when debugging. I opt for the "cleaner" and easier to debug approach by creating two different `BookController`s in different namespaces. – bassxzero Dec 25 '20 at 01:13
  • share the code of index method. – Jasim Juwel Dec 25 '20 at 06:58

2 Answers2

2

You can even keep the route the same. Just use roles for teachers and students.

If you don't want to implement roles you can just have a column on users table for the type i.e. teacher or student.

Then when the request hits the controller method, depending upon the type of user you can write a query to either get all books for teacher or just the books allocated to the user in case of a student.

public function index()
{
    $user = auth()->user();
    $query = Book::query();

    if($user->type === 'student') {
        $query->where(/*some condition to constrain the records*/);

        //Or if you have a relation to fetch books for the user
        $query = User::query()->with('books');

    }

    $books = $query->get();

    //return json response for ajax request or a view
}
Donkarnash
  • 12,433
  • 5
  • 26
  • 37
1

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

Abdul Hakeem
  • 405
  • 4
  • 11