I have this SQL query:
SELECT * FROM books WHERE id IN
(SELECT book_id FROM `author_book` WHERE author_book.author_id IN
(SELECT id FROM authors WHERE self_name like "%ори%" OR father_name LIKE "%ори%" OR family_name LIKE "%ори%"))
It works, but I need convert to Laravel eloquent. I try this:
DB::select("SELECT * FROM books WHERE id IN "
. "(SELECT book_id FROM `author_book` WHERE author_book.author_id IN "
. "(SELECT id FROM authors WHERE self_name like '%" . $attributes['search'] . "%' "
. "OR father_name LIKE '%" . $attributes['search'] . "%' "
. "OR family_name LIKE '%" . $attributes['search'] . "%'))");
And this works, but can't use pagination. Does anyone have any idea?
If you want Eloquent solution, use the
whereHas()
method:This will work if you properly defined relationships. In the
Book
model:And in the
Author
model: