I have a search method that searches for multiple models. For simplicity, I have added two models that I am looking for.
I would like to combine the two models to break down their results.
This is what I am doing right now.
public function search(Request $request) { $query = $request->get('q'); $threads = Thread::where('title', 'LIKE', "%{$query}%")->get(); $posts = Post::where('body', 'LIKE', "%{$query}%")->get(); $results = array_merge($threads->toArray(), $posts->toArray()); $results = new Paginator($results, 10); return view('pages.search', compact('query', 'results')); }
It works, but I feel that it is really inefficient and can be improved. Is there a better way to do this?
source share