1. Go to this page and download the library: Download jdkweb/search library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
jdkweb / search example snippets
'settings' => [
'[CONFIG-NAME]' => [ // Engine configuration name (standard preset: 'default')
'searchQuery' => '[PRESET SEARCH]', // Optional: preset search words, results directly shown
'pagination' => 20, // Optional: search items per page , default = 15
'parameters' => [ // Optional: specific names query strings parameters
'search_query' => '[NAME]', // search terms, default: q
'actual_page' => '[NAME]', // result page, default: p
'actual_filter' => '[NAME]' // result filter, default: f
],
// Model configuration
...
]
]
$search = app('search') // With default settings
->setSearchQuery('Adobe'); // Optional: preset search words, results directly shown
->setPagination(10) // Optional: search items per page , default = 15
->setParams([ // Optional: specific names query strings parameters
'search_query' => 'search', // search terms
'actual_page' => 'page', // result page
'actual_filter' => 'filter' // result filter
])
->setModel(\App\Models\Articles::class, [ // Database Article table to search in
'title' => 2, // Fieldnames with priority (extra weight)
'lead' => 1.5,
'body' => 1,
])
->setConditions(\App\Models\Articles::class, [
'active' => 1, // active must be true
'created_at:>' => '01-01-2025', // articles created after 01 jan. 2025
])
->showResults(\App\Models\Articles::class, [
'title' => 'name', // pass database field = 'name' as 'title' to results
'lead' => 'intro', // pass database field = 'intro' as 'lead' to results
'url' => 'getSlug' // method in \App\Models\Articles pass as 'url' to results
])
->setModel(\App\Models\User::class, [ // Database User table to search in
'name' => 1, // Fieldnames with priority (extra weight)
'email' => 1,
])
->setConditions(\App\Models\User::class, [
'active' => 1, // active must be true
'public:in' => function () { // public value must be available, from Public model
return App\Models\Public::query()
->where('active', 1)
->select('id');
}
])
->showResults(\App\Models\User::class, [
'title' => function () { // show name of user with the company name (from an other model)
$company_name = App\Models\Companies::query()
->where('user_id', $this->id)
->select('company_name')->first()->company_name
return $this->name . " (" . $company_name .")"
},
'lead' => 'intro',
'url' => fn() => '/users/' . $this->slug // closure arrow function to get slug passed as url to result
]);
}
search?q=some+search+words
$search = app('search'); // search with 'default' settings
OR
$search = app('search', ['default']); // search with 'default' settings
// search result
$result = $search->get(); // returning a collection
$search = app('search', ['global']); // search with 'global' settings
// search result
$result = $search->get(); // returning a collection
#items: Illuminate\Support\Collection {#3470 ▼
#items: array:15 [▼
0 => Illuminate\Support\Collection {#2268 ▶}
1 => Illuminate\Support\Collection {#3511 ▶}
2 => Illuminate\Support\Collection {#3260 ▶}
3 => Illuminate\Support\Collection {#3076 ▼
#items: array:3 [▼
"title" => "Sed non leo ac massa dignissim condimentum"
"lead" => "Donec efficitur dictum justo vitae auctor. Curabitur eu diam a nisi eleifend tristique eget non augue. Integer sed metus non nisl fringilla venenatis."
"url" => "/articles/sed-non-leo-ac-massa-dignissim",
"date" => "11/01/2025"
]
#escapeWhenCastingToString: false
}
4 => Illuminate\Support\Collection {#2335 ▶}
5 => Illuminate\Support\Collection {#2554 ▶}
....
]
}
$search = app('search', '[CONFIG-NAME]');
$searchResult = $search->get();
// Handle next search via GET-variable (searchQuery is overwritten)
// OR combine config with direct settings
$search->setSearchQuery([NEW SEARCH_WORDS]);
$newsearchResult = $search->get();
$searchResult = $search->get();
// Handle next search via GET-variable (setSearchQuery is overwritten)
// OR insert new search words
$search->setSearchQuery([NEW SEARCH WORDS]);
$newsearchResult = $search->get();