Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Mike_91's avatar

Simple search in laravel

Hi i want to create a simple search form for my posts for example i want different search inputs and select boxes so to search by id, title, category etc do you have any idea?

0 likes
2 replies
haruntunay's avatar

You need a form that makes a get request.

Basically, something like this:

<form action="/index", method="GET">
    <input name="title" placeholder="title">
    <input name="category" placeholder="category">

    etc...
</form>

<!-- action="/index" part telling this form should make a get request to yourdomain/index-->

Then, in your controller which responds to the "yourdomain/index", you can get the params passed in using php's super global "$_GET" then, query your database with the passed in values.

For example,


    $title = $_GET['title'];
    $category = $_GET['category'];

    $items = \App\Item::where([ 
        ['title', 'LIKE', '%' . $title . '%'],
        ['category', 'LIKE', '%' . $category . '%'],
    ])->get();

    return view('view_name', compact('items'));

4 likes

Please or to participate in this conversation.