This document describes the advanced search functionality implemented for users, posts, and groups.
The advanced search system provides powerful filtering capabilities across three main entities:
- Users: Search by name, email, verification status, role, and creation date
- Posts: Search by title, content, status, author, and publication date
- Groups: Search by name, description, type, owner, and creation date
All search endpoints are accessible via the /api/search/ prefix:
Endpoint: GET /api/search/users
Query Parameters:
query(string, optional): Search term for name or emailrole(string, optional): Filter by user roleverified(boolean, optional): Filter by email verification status (0 = unverified, 1 = verified)created_from(date, optional): Filter users created after this datecreated_to(date, optional): Filter users created before this dateorder_by(string, optional): Sort field (name, email, created_at) - default: created_atorder_direction(string, optional): Sort direction (asc, desc) - default: descper_page(integer, optional): Results per page (1-100) - default: 15
Example Requests:
# Search for users with "john" in name or email
GET /api/search/users?query=john
# Search for verified users
GET /api/search/users?verified=1
# Search for users created in the last 30 days, sorted by name
GET /api/search/users?created_from=2026-01-15&order_by=name&order_direction=asc
# Combined filters
GET /api/search/users?query=developer&verified=1&role=admin&per_page=20Endpoint: GET /api/search/posts
Query Parameters:
query(string, optional): Search term for title or contentstatus(string, optional): Filter by status (draft, published, archived)author_id(integer, optional): Filter by author user IDpublished_from(date, optional): Filter posts published after this datepublished_to(date, optional): Filter posts published before this dateinclude_drafts(boolean, optional): Include draft posts (default: false - only published posts)order_by(string, optional): Sort field (title, published_at, created_at) - default: published_atorder_direction(string, optional): Sort direction (asc, desc) - default: descper_page(integer, optional): Results per page (1-100) - default: 15
Example Requests:
# Search for posts containing "Laravel"
GET /api/search/posts?query=Laravel
# Search for published posts by specific author
GET /api/search/posts?author_id=123&status=published
# Search for posts published in the last week
GET /api/search/posts?published_from=2026-02-07
# Include draft posts in search
GET /api/search/posts?query=tutorial&include_drafts=1
# Combined filters
GET /api/search/posts?query=API&status=published&author_id=5&order_by=titleEndpoint: GET /api/search/groups
Query Parameters:
query(string, optional): Search term for name or descriptiontype(string, optional): Filter by type (public, private, restricted)owner_id(integer, optional): Filter by owner user IDcreated_from(date, optional): Filter groups created after this datecreated_to(date, optional): Filter groups created before this dateorder_by(string, optional): Sort field (name, created_at) - default: created_atorder_direction(string, optional): Sort direction (asc, desc) - default: descper_page(integer, optional): Results per page (1-100) - default: 15
Example Requests:
# Search for groups containing "developer"
GET /api/search/groups?query=developer
# Search for public groups
GET /api/search/groups?type=public
# Search for groups owned by specific user
GET /api/search/groups?owner_id=456
# Combined filters
GET /api/search/groups?query=Laravel&type=public&order_by=name&order_direction=ascEndpoint: GET /api/search/all
Query Parameters:
query(string, optional): Search term applied to all entity typestypes(array, optional): Limit search to specific types (users, posts, groups) - default: all typesper_page(integer, optional): Results per page for each type (1-100) - default: 5
Example Requests:
# Search all entities for "Laravel"
GET /api/search/all?query=Laravel
# Search only posts and groups
GET /api/search/all?query=tutorial&types[]=posts&types[]=groups
# Limit results per type
GET /api/search/all?query=developer&per_page=10Response Format:
{
"users": {
"data": [...],
"current_page": 1,
"total": 5
},
"posts": {
"data": [...],
"current_page": 1,
"total": 12
},
"groups": {
"data": [...],
"current_page": 1,
"total": 3
}
}- id (primary key)
- title (string, indexed)
- content (text)
- author_id (foreign key to users, indexed)
- status (enum: draft, published, archived, indexed)
- published_at (datetime, nullable, indexed)
- created_at, updated_at, deleted_at (timestamps)
- id (primary key)
- name (string, indexed)
- description (text, nullable)
- owner_id (foreign key to users, indexed)
- type (enum: public, private, restricted, indexed)
- created_at, updated_at, deleted_at (timestamps)
Located at: app/Models/Post.php
Relationships:
author(): BelongsTo User
Query Scopes:
published(): Only published postsstatus($status): Filter by statusbyAuthor($authorId): Filter by authordateRange($from, $to): Filter by publication date rangesearch($query): Search by title or content
Located at: app/Models/Group.php
Relationships:
owner(): BelongsTo User
Query Scopes:
type($type): Filter by typebyOwner($ownerId): Filter by ownersearch($query): Search by name or description
Located at: app/Models/User.php
New Relationships:
posts(): HasMany Postgroups(): HasMany Group
New Query Scopes:
search($query): Search by name or email
Located at: app/Services/SearchService.php
Methods:
searchUsers(array $filters): Search users with advanced filterssearchPosts(array $filters): Search posts with advanced filterssearchGroups(array $filters): Search groups with advanced filterssearchAll(array $filters): Search all entities simultaneously
Comprehensive test suites are located in tests/Feature/:
Tests for user search functionality including:
- Search by name
- Search by email
- Partial matching
- Filtering by verification status
- Date range filtering
- Sorting
- Pagination
- Validation
Tests for post search functionality including:
- Search by title
- Search by content
- Filtering by status
- Filtering by author
- Date range filtering
- Draft exclusion/inclusion
- Sorting
- Pagination
- Validation
Tests for group search functionality including:
- Search by name
- Search by description
- Filtering by type
- Filtering by owner
- Date range filtering
- Sorting
- Pagination
- Validation
Tests for combined search functionality including:
- Search across all entities
- Filtering by specific entity types
- Per-page limits
- Empty results handling
To run all search tests:
php artisan test --filter=SearchTo run specific test suites:
php artisan test tests/Feature/SearchUsersTest.php
php artisan test tests/Feature/SearchPostsTest.php
php artisan test tests/Feature/SearchGroupsTest.php
php artisan test tests/Feature/SearchAllTest.php-
Run Migrations:
php artisan migrate
-
Seed Test Data (optional):
php artisan db:seed --class=PostSeeder php artisan db:seed --class=GroupSeeder
-
Test the API: Use a tool like Postman, curl, or your application's frontend to test the endpoints.
- All searchable fields are indexed for optimal performance
- Pagination is enforced with a maximum of 100 results per page
- The search uses LIKE queries with wildcards, suitable for small to medium datasets
- For larger datasets, consider integrating Laravel Scout with Meilisearch or Algolia
Potential improvements:
- Full-text search integration (Laravel Scout)
- Search highlighting
- Saved searches/filters
- Search analytics
- Elasticsearch integration for advanced search
- Real-time search suggestions
- Advanced boolean search operators