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
Please or to participate in this conversation.