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

Jecs9's avatar
Level 1

How to pass current input value as URL parameter?

I am not sure how to search for information about the solution I am trying to make so I apologize if this is a replicate of another post or if this is not the correct forum (I was also thinking to place it in Elocuent)

I have a simple form in my index.blade.php

<form method="POST" action="{{ route('articles.index', ['search' => $search]) }}">
@method('PUT')
@csrf
    <div class="field">
        <label class="label blog-sidebar-label" for="search">
            <input class="search-field" type="search" name="search" id="search" placeholder="Buscar..." value="{{ $search }}">
        </label>
    </div>
</form>

I want to pass the value of the input "search" as a parameter of the URL

The route articles.index is "/blog" so what I want to have as result is "MyURL/blog?search=ValueOfSearch"

With the current code in the form, I have managed to show the parameter ?search= after I send the form but it always takes the previous value of $search

my controller looks like this

public function index()
    {
        $search = '';
        if (request('search'))
        {
            $articles = Article::where('title', 'LIKE', '%'.request('search').'%')->paginate(20);
            $search = request('search');
        }
        elseif (request('tag'))
        {
            $articles = Tag::where('name', request('tag'))->firstOrFail()->articles()->paginate(20);
        }
        else {
	        $articles = Article::latest()->paginate(20);
        }
        $tags = Tag::orderBy('name')->get();

        //return dd($articles);
        return view('articles.index', ['articles' => $articles, 'tags' => $tags, 'search' => $search]);
    }

With this the first time I search something I get the URL => MyURL/blog?=

The second time I search something I get the URL => MyURL/blog?=PreviousSearchString

Is there any way to pass the current value from the request directly as an URL parameter?

Thank you in advance for your support

Have a good day

0 likes
2 replies
Snapey's avatar
Snapey
Best Answer
Level 122

GET requests pass parameters in the URL

POST requests send the parameters in the request body.

Therefore change your search form to GET, and remove @csrf and @method('put')

You would also need to change the Route to GET also Route::get()

Jecs9's avatar
Level 1

Thank you for your fast reply, I have done it and it works! There is a new issue though, whenever the search string is empty now I see always MyURL/blog?search= Is there a way to not add search= if the string is empty?

Please or to participate in this conversation.