Search for Multiple Query Items at Once in Controller

How do you input to query multiple YAML data items at once?

Current Controller:

<?php

return function ($site) {

  $query   = get('q');
  $results = page('items-for-sale')->search($query, 'itemnumber');
  $results = $results->paginate(15);

  return [
    'query'   => $query,
    'results' => $results,
    'pagination' => $results->pagination()
  ];

};

I was thinking the following but it doesn’t work:

<?php

return function ($site) {

  $query   = get('q');
  $results = page('items-for-sale')->search($query, 'itemnumber', 'shortdescription', 'fulldescription');
  $results = $results->paginate(15);

  return [
    'query'   => $query,
    'results' => $results,
    'pagination' => $results->pagination()
  ];

};

I figured it out

<?php

return function ($site) {

  $query   = get('q');
  $results = page('items-for-sale')->search($query, 'itemnumber|shortdescription|fulldescription');
  $results = $results->paginate(15);

  return [
    'query'   => $query,
    'results' => $results,
    'pagination' => $results->pagination()
  ];

};

hi jonathan,

in case you need the search to match terms by AND not ANY then you can chain the search on the results again and again. like must match “hello world” and not “hello” or “world”.

<?php

return function ($site) {
  // AND-based search
  $queryParts = explode(' ', $query);
  $first = array_shift($queryParts);
  $results = page('items-for-sale')
     ->search($first, 'itemnumber|shortdescription|fulldescription');
  foreach ($queryParts as $q) {
    $results = $results->search($q, 'itemnumber|shortdescription|fulldescription');
  }
  // ...

};
1 Like

Thanks for this. It amazes me just how much Kirby can do, phenomenal product!