1

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?

Code

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);
}

2 Answers 2

1

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);

}

Sign up to request clarification or add additional context in comments.

2 Comments

can i have them all under result array? let say results[ posts[],products[],etc[]. ]
yes but at the firat i was just getting 9 arrays then i changed your code now i have one array and 9 arrays under it.
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.