1

I am trying to retrieve search suggestions for a search form. I sent the parameter entered by the user, but how do I do it in the criteria? I tried this:

'criteria' => [
    'search' => 'title: *'.Craft::$app->request->getParam("key").'*'
],

but it does not return any results. What am I doing wrong? this is my entire endpoint:

'suggests' =>function(){
        return [
            'elementType' => Product::class,
            'paginate' => false,
            'pretty' => true,
            'criteria' => [
                'limit' => 15,
                'search' => 'title: *'.Craft::$app->request->getParam("key").'*'
            ],
            'transformer' => function(Product $product)  {
                return [
                    'title' => $product->title,
                    'url' => $product->url
                ];
            },
        ];
    },
  • Got it. LOL 'criteria' => [ 'title' => ''.Craft::$app->request->getParam("key").'' ], that was fast. – Flying Car 0013 Dec 11 '18 at 10:45
  • 1
    Glad you got it sorted! Would you mind adding that as an official answer in case it ends up helping someone in the future? – Brad Bell Dec 12 '18 at 5:05
1

I got it.

'criteria' => [ 'title' => '*'.Craft::$app->request->getParam("key").'*' ]
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.