I have library-like system. In one table bunch of books in different copies and other table for reservations.
Books table (for simplisity only three columns)
id | title | inventory_number |
---|---|---|
1 | Coding is awesome | 1111 |
2 | Coding is awesome | 2222 |
3 | Coding is awesome | 3333 |
4 | Get rich quick | 4444 |
5 | Get rich quick | 5555 |
6 | YOLO | 6666 |
Reservation table (for simplisity only three columns)
id | book_id | user_id |
---|---|---|
1 | 1 | 1 |
2 | 1 | 2 |
3 | 2 | 1 |
4 | 4 | 1 |
5 | 5 | 2 |
Although in Books table is 6 books, to end user it means that there is only 3 books - "Coding is awesome", "Get rich quick" and "YOLO" and he don't need to know nothing about other book copies in stock.
When user comes to "Library/Store" and asks for some book than he should get the first book with minimum reservations for current book.
From given data above can be understood that:
- "Coding is awesome" (ID:1) is reserved 2 times
- "Coding is awesome" (ID:2) is reserved 1 times
- "Coding is awesome" (ID:3) is reserved 0 times
So when user asks for "Coding is awesome" book he needs to get book with ID:3 And when he asks for "Get ritch quick" he gets one of two books because all two books is reserved equal number of times.
I need a solution for "end user" that will return only books with different (unique) title and other to current book related data
Although in Books table is 6 books, In this case need to return three books:
id | title | inventory_number |
---|---|---|
3 | Coding is awesome | 3333 |
4 | Get rich quick | 4444 |
6 | YOLO | 6666 |
If there will be more books with diferent names then they also need to be shown. (for each unique title one record)
If it is possible then I need to return these data from database using Eloquant or Query Builder not filter out with Collections to save resources and use pagging.
Using Laravel 8, Mysql 5.7