Reputation: 7128
I have search function where it searches into several model and get results back, now i would like to have all those model results into one variable so i can use only one variable in my blade.
How can i do that?
public function results(Request $request) {
$q = $request->input('q');
$products = Product::where('name', 'like', "%{$q}%")
->orWhere('body', 'like', "%{$q}%")
->paginate(9);
$posts = Post::where('name', 'like', "%{$q}%")
->orWhere('body', 'like', "%{$q}%")
->paginate(9);
$pages = Page::where('name', 'like', "%{$q}%")
->orWhere('body', 'like', "%{$q}%")
->paginate(9);
$tags = Tag::where('name', 'like', "%{$q}%")
->paginate(9);
$jobs = Job::where('name', 'like', "%{$q}%")
->orWhere('body', 'like', "%{$q}%")
->paginate(9);
$categories = Category::where('name', 'like', "%{$q}%")
->orWhere('body', 'like', "%{$q}%")
->paginate(9);
$listings = Listing::where('name', 'like', "%{$q}%")
->orWhere('body', 'like', "%{$q}%")
->paginate(9);
$portfolios = Portfolio::where('name', 'like', "%{$q}%")
->orWhere('body', 'like', "%{$q}%")
->paginate(9);
$services = Service::where('name', 'like', "%{$q}%")
->orWhere('body', 'like', "%{$q}%")
->paginate(9);
return view('front.pages.search', compact('products', 'q', 'pages', 'posts', 'tags', 'jobs', 'categories', 'listings', 'portfolios', 'services'))->withQuery($q);
}
Upvotes: 1
Views: 124
Reputation: 13394
Try this one make codes cleaner,
First defined the array of all your models names.
Secondly, loop this models' names, concat with namespace then query on it.
Thirdly, convert the model name to plural and camel form as key, and put the result in with this key.
use Illuminate\Support\Str;
...
$namespace = 'App\\';
$models = ["Product", "Post", "Page", "Tag", "Job", "Category", "Listing", "Portfolio", "Service"];
$data = array();
foreach($models as $model) {
$key = str_plural(Str::camel($model));
$data[$key] = ($namespace.$model)::where('name', 'like', "%{$q}%")
->orWhere('body', 'like', "%{$q}%")
->paginate(9);
}
dd($data);
Upvotes: 0
Reputation: 1374
You can store the results in an assoc array instead of separate variables.
public function results(Request $request) {
$q = $request->input('q');
$result = [];
$result['products'] = Product::where('name', 'like', "%{$q}%")
->orWhere('body', 'like', "%{$q}%")
->paginate(9);
$result['posts'] = Post::where('name', 'like', "%{$q}%")
->orWhere('body', 'like', "%{$q}%")
->paginate(9);
$result['pages'] = Page::where('name', 'like', "%{$q}%")
->orWhere('body', 'like', "%{$q}%")
->paginate(9);
$result['tags'] = Tag::where('name', 'like', "%{$q}%")
->paginate(9);
$result['jobs'] = Job::where('name', 'like', "%{$q}%")
->orWhere('body', 'like', "%{$q}%")
->paginate(9);
$result['categories'] = Category::where('name', 'like', "%{$q}%")
->orWhere('body', 'like', "%{$q}%")
->paginate(9);
$result['listings'] = Listing::where('name', 'like', "%{$q}%")
->orWhere('body', 'like', "%{$q}%")
->paginate(9);
$result['portfolios'] = Portfolio::where('name', 'like', "%{$q}%")
->orWhere('body', 'like', "%{$q}%")
->paginate(9);
$result['services'] = Service::where('name', 'like', "%{$q}%")
->orWhere('body', 'like', "%{$q}%")
->paginate(9);
return view('front.pages.search', compact($result))->withQuery($q);
}
Upvotes: 1