Use whereHas Builder method.
Feb 2, 2022
3
Level 1
Filter model by value of related object (one to many)
Hello, my first time here :D I have models: Book, BookRoute and Users. Users can register the route of the book then row with id, user_id, book_id and date is created. I need to show only books which were registered by the current user as last (BookRoute with this user_id is the last for this book). I want to do this in livewire, so the best option I found is macro for Builder (then I can paginate etc.). How can I write this macro? Can I use it in this way:
‘books’ => Book::when($only_current_user_books, fun($query){ return …. }->(second when condition)->paginate(10); ???
Please or to participate in this conversation.