Laravel 5.3 Search function in API

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering Dynamic Searching in Laravel APIs: Querying Based on User Input

As developers building modern applications, one of the most common requirements for an API is the ability to search and filter data dynamically. When users interact with a frontend or mobile application, they expect fast, relevant results based on what they type—whether it's a name, a registration ID, or any other attribute.

The scenario you described—implementing a search function where users input data and expect a JSON response—is a classic challenge in API development. As a senior developer, I can tell you that the initial approach of fetching all records (Driver::all()) is fundamentally inefficient for large datasets. We need to leverage Eloquent's powerful querying capabilities to filter the results directly at the database level.

This post will guide you through the correct, efficient, and scalable way to implement dynamic searching in your Laravel API using Eloquent.


The Problem with Fetching Everything: Inefficiency Matters

Your initial controller implementation fetches every single record regardless of what the user searched for:

public function getSearchResults(Request $request) {
    $search_drivers = Driver::all(); // This retrieves ALL drivers
    return Response::json([
        'data' => $search_drivers->all() 
    ]);
}

This approach forces the application to load potentially thousands of records into memory and then filter them in PHP. This is slow, consumes excessive bandwidth, and provides a terrible user experience. The key to efficient API design is pushing the filtering logic down to the database using Eloquent's where() clauses.

Strategy: Dynamic Query Building with Eloquent

To handle dynamic input (like searching across multiple columns), you need to inspect the request parameters, determine which fields the user is searching against, and construct a conditional query.

Let’s assume your user inputs a search term via the URL query parameter, for example: ?search=John or perhaps more specifically, using the structure you proposed: ?data=first_name:John.

Step 1: Parsing the Request Input

First, you need to parse the incoming request to extract the search terms and map them to the columns in your Driver model. A flexible approach is to check if the input contains a colon (:) or simply treat it as a general search term across relevant fields.

For this example, we will focus on searching within specific columns like first_name and last_name.

Step 2: Implementing Dynamic Queries in the Controller

We will modify your controller to dynamically build the query based on the request input. We use dynamic where clauses, which are crucial for performance.

Here is how you can restructure your getSearchResults method:

use Illuminate\Http\Request;
use App\Models\Driver; // Ensure you import your model

class DriversController extends Controller
{
    public function getSearchResults(Request $request)
    {
        // 1. Validate and sanitize input
        $searchTerm = $request->input('data'); // Assuming 'data' is the parameter sent

        if (!$searchTerm) {
            return response()->json(['message' => 'Search term is required.'], 400);
        }

        // 2. Dynamically build the query array
        $query = Driver::query();
        $searchFields = [];

        // --- Example of dynamic field selection (Adapt this logic based on your exact input format) ---
        if (str_contains($searchTerm, ':')) {
            // Handle specific column searches: e.g., data=first_name:John
            [$field, $value] = explode(':', $searchTerm, 2);
            $searchFields[] = [$field, $value];
        } else {
            // Handle general search (e.g., searching across first_name OR last_name)
            // This requires a more complex setup using whereRaw or nested where clauses.
            // For simplicity here, let's implement specific field searching:
             $searchFields[] = ['first_name', $searchTerm]; // Search by first name only for this example
        }


        // 3. Apply the WHERE clauses dynamically
        foreach ($searchFields as [$field, $value]) {
            // Use where() to safely filter results in the database
            $query->where($field, 'LIKE', "%{$value}%"); // Use LIKE for partial matching
        }

        // 4. Execute the query and return JSON
        $searchResults = $query->get();

        return response()->json([
            'status' => 'success',
            'data' => $searchResults
        ]);
    }
}

Best Practices for API Searching

  1. Use LIKE for Partial Matches: When searching across text fields (like names or descriptions), always use the LIKE operator with wildcards (%) to allow for partial string matches. This is essential for user-friendly search experiences.
  2. Index Your Database: For any field you frequently search on, ensure that you have appropriate database indexes defined. This dramatically speeds up query execution time, especially as your table grows.
  3. Pagination is Key: Never return the entire dataset if there are many results. Always implement pagination using the paginate() method provided by Eloquent. This keeps API responses small and manageable.
  4. Laravel Ecosystem: For complex searching that involves full-text search across multiple columns, consider integrating specialized tools or leveraging database features like PostgreSQL's full-text search capabilities. The Laravel framework provides excellent tools for managing these interactions, making development much smoother, similar to how you manage relationships in a well-structured application on laravelcompany.com.

Conclusion

By shifting your focus from fetching all data to dynamically constructing Eloquent queries using where() clauses based on user input, you transform your API from a simple data retriever into a powerful, dynamic search engine. This approach ensures performance, scalability, and a robust experience for your frontend and mobile applications. Start by mastering the dynamic query building techniques, and you will be well on your way to building highly functional APIs.