How to create search api using laravel

In this tutorial, I will give you an example of the “How to Create Search API in Laravel“, So you can easily apply it with your laravel 5, laravel 6, laravel 7, and laravel 8 application.

First, what we’re doing here, This is the example :

Product Table :

We have created a products table and we have some products for testing in the table in the database.

Product Model :

We have a Product Model, we will get data from it.



namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
    protected $table = 'products';
    protected $fillable = ['id','product_no','cat_id','name','description'];


We will search data or strings from our route and get this parameter from the search method in the controller.

Create Route :



Controller :



namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Product;

class ProductController extends Controller
    function search($name)
        $result = Product::where('name', 'LIKE', '%'. $name. '%')->get();
         return Response()->json($result);
        return response()->json(['Result' => 'No Data not found'], 404);

Setup Postman for API :

Postman is an API platform for building and using APIs. We have already Installed Postman for testing API you can use any platform for testing APIs.

Select the Get method here, and just put the URL here,{keyword}

After following these steps successfully, you are able to fetch related search keyword results from the products table.

In this article, we learned “Create a product search API in Laravel”, I hope this article will help you with your Laravel application Project.

Leave a Comment

Your email address will not be published. Required fields are marked *