Добрый день! Код такой:
Plugin.php
Event::listen('offline.sitesearch.query', function ($query) {
// The controller is used to generate page URLs.
$controller = \Cms\Classes\Controller::getController() ?? new \Cms\Classes\Controller();
// Search your plugin's contents
$items = Models\Post::where('title', 'like', "%${query}%")->orWhere('content', 'like', "%${query}%")->get();
// Now build a results array
$results = $items->map(function ($item) use ($query, $controller) {
// If the query is found in the title, set a relevance of 2
$relevance = mb_stripos($item->title, $query) !== false ? 2 : 1;
if ($item->featured_images) {
return [
'title' => $item->title,
'text' => $item->content,
'url' => $controller->pageUrl('/news', ['slug' => $item->slug]),
'thumb' => optional($item->featured_images)->first(), // Instance of System\Models\File
'relevance' => $relevance, // higher relevance results in a higher
// position in the results listing
// 'meta' => 'data', // optional, any other information you want
// to associate with this result
//'model' => $item, // optional, pass along the original model
];
} else {
return [
'title' => $item->title,
'text' => $item->content,
'url' => $controller->pageUrl('/post', ['slug' => $item->slug]),
'relevance' => $relevance, // higher relevance results in a higher position in the results listing
// 'meta' => 'data', // optional, any other information you want to associate with this result
// 'model' => $item, // optional, pass along the original model
];
}
});
return [
'provider' => '', // The badge to display for this result
'results' => $results,
];
});