Skip to content

Instantly share code, notes, and snippets.

@tilhom
Created November 23, 2018 11:33
Show Gist options
  • Save tilhom/c50badb6a384afb6c76abfa9fd9889dc to your computer and use it in GitHub Desktop.
Save tilhom/c50badb6a384afb6c76abfa9fd9889dc to your computer and use it in GitHub Desktop.
laravel search with
$categories = Categories::whereHas('products', function ($query) use ($searchString){
$query->where('name', 'like', '%'.$searchString.'%');
})
->with(['products' => function($query) use ($searchString){
$query->where('name', 'like', '%'.$searchString.'%');
}])->get();
foreach($categories as $category){
echo $category->name . ':' . PHP_EOL;
foreach($category->products as $product){
echo . '-' . $product->name . PHP_EOL;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment