Search in Laravel

Iqbal Ramadhani
2 min readNov 5, 2023

Search functionality is a fundamental feature that allows users to quickly find the information they need. Laravel can provide a robust foundation for building search capabilities into your web application. To create a basic search functionality in a Laravel application, you can follow these steps. Here’s how to create a simple example of searching for data in a database table.

1. Setup Laravel Project
If you haven’t already, create a Laravel project by using the Laravel installer or Composer. For example:

composer create-project --prefer-dist laravel/laravel my-search-app

2. Create a Model
Create a model for the data you want to search. For example, if you want to search for products, create a Product model:

php artisan make:model Product

3. Set up Database
Configure your database connection in the .env file and run migrations to create the database table:

php artisan migrate

4. Populate Data
Insert some sample data into your database table. You can use Laravel’s database seeding functionality for this.

5. Create a Controller
Create a controller to handle the search functionality. You can generate a controller using the following command:

php artisan make:controller ProductController

6. Define the Search Method in the Controller
In your ProductController, define a method to perform the search:

public function search(Request $request)
{
$search = $request->input('search');
$results = Product::where('name', 'like', "%$search%")->get();

return view('products.index', ['results' => $results]);
}

In this example, we use a simple like query to search for products whose name contains the user's query.

7. Create a Search Form
Create a form in your view to input the search query. For example, in resources/views/products/index.blade.php:

<form action="{{ route('products.search') }}" method="GET">
<input type="text" name="search" placeholder="Search Products">
<button type="submit">Search</button>
</form>

8. Define Routes
In your routes/web.php file, define a route for the search:

Route::get('/', 'ProductController@index');
Route::get('/search', 'ProductController@search')->name('products.search');

9. Display Search Results
Modify your index view (resources/views/products/index.blade.php) to display the search results. For example:

@if (count($results) > 0)
<ul>
@foreach ($results as $result)
<li>{{ $result->name }}</li>
@endforeach
</ul>
@else
<p>No results found.</p>
@endif

10. Run Your Application
Start your Laravel development server:

php artisan serve

You can now access your application in a web browser and use the search functionality.

Conclusion

Implementing search functionality in a Laravel application is a common and essential feature that enhances the user experience. Laravel’s robust framework makes it relatively straightforward to set up basic search functionality, as demonstrated in this article. Depending on your specific requirements, you can extend and customize the search feature with additional features like pagination, filtering, and sorting.

--

--