1. Go to this page and download the library: Download provydon/laravel-pgsearch 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/ */
provydon / laravel-pgsearch example snippets
// Search users
User::query()->pgSearch('john doe', ['name', 'email'])->get();
// Search with relationships
Post::query()->pgSearch('jane', ['title', 'user.name'])->get();
// Phone number search
User::query()->pgSearch('1234567890', ['phone'])->get();
// Or use the helper function
pg_search(User::query(), 'john doe', ['name', 'email'])->get();
// Search posts by author name
Post::query()->pgSearch('jane doe', ['title', 'user.name'])->get();
// Search orders by customer info
Order::query()->pgSearch('smith', ['number', 'customer.name', 'customer.email'])->get();
// ✓ Relevance first, then created_at
User::query()->pgSearch('john', ['name'])->orderBy('created_at', 'desc')->get();
// ✗ Your order wins; relevance only as tiebreaker
User::query()->orderBy('created_at')->pgSearch('john', ['name'])->get();
// Using the helper function
$users = pg_search(User::query(), 'john doe', ['name', 'email'])->get();
// With options
$users = pg_search(User::query(), 'john', ['name'], ['normalize' => false])->get();
// In controllers
public function search(Request $request)
{
$query = User::query()->where('active', true);
if ($request->has('search')) {
$query = pg_search($query, $request->search, ['name', 'email']);
}
return $query->paginate(15);
}
// config/pgsearch.php
return [
'normalize' => true, // Enable smart text matching (punctuation-stripped)
'order_by_best_match' => true, // Order results by relevance (exact match > normalized > word matches)
// Word-based matching (on normalized text)
// When enabled, the search term is split into tokens and each
// significant word is also searched individually. This lets
// "Lagos State" match a record that only contains "Lagos", etc.
'word_based_matching' => true,
// NEW: Common suffixes ignored as standalone tokens when doing
// word-based matching. Useful for geographic names:
// "Lagos State" → token "lagos" (since "state" is ignored).
'ignore_suffixes' => [
'state',
'province',
'region',
'territory',
'city',
'town',
'municipality',
],
];
// These all find the same user:
User::query()->pgSearch('Jane Doe', ['name'])->get(); // Direct match
User::query()->pgSearch('jane doe', ['name'])->get(); // Case insensitive
User::query()->pgSearch('janedoe', ['name'])->get(); // Normalized match
// Phone number variations:
User::query()->pgSearch('1234567890', ['phone'])->get(); // Finds all these:
// "(123) 456-7890", "123-456-7890", "123.456.7890", "123 456 7890"