php - Search in multiple tables by eloquent query -
i have 2 tables: posts
, pages
. based on keyword, want search different columns of 2 tables occurrence of keyword. 2 tables not have related content.
i have written 2 queries 1 each table. following queries:
$result1 = post::where('title_en', 'like', '%' . $keyword . '%') ->or_where('title_np', 'like', '%' . $keyword . '%') ->order_by('id', 'desc') ->paginate(10); $result2 = page::where('title', 'like', '%' . $keyword . '%') ->or_where('content', 'like', '%' . $keyword . '%') ->order_by('id', 'desc') ->paginate(10);
the above queries return 2 different laravel\paginator
object. want single laravel\paginator
object single pagination displayed on page works both queries or on single query achieves functionality of both above queries. how able that?
since unrelated tables, need trickery that's complex eloquent handle, need similar following join 2 queries together, , still able limit / ordering on combined queries:
$results = db::query('select id, title ( select id, title `post` `title_np` "%'.$keyword.'%" or `title_en` "%'.$keyword.'%" union select id, title `page` `title` "%'.$keyword.'%" or `content` "%'.$keyword.'%" ) temp_table order `id` desc limit 0, 10 ')
nb above untested, purely example of approach you'll need take, not sure work.
Comments
Post a Comment