John Davidson

php - How to shift _token to the last of the url in laravel

0 comments
Message:


Hello I want to shift the _token in the last of URL asshown below


What I am getting:



example.com/search?_token=qkPc5aNyEp7tysbyQhZcnjHdP1wi9q&query=php



What I want:



example.com/search?query=php&_token=qkPc5aNyEp7tysbyQhZcnjHdP1wi9q



My Form code Is like


<form action="search" method="GET">
{!! csrf_field() !!}
<div class="main-search-input fl-wrap">
<div class="main-search-input-item">
<input type="text" name="query" value="" placeholder="Search snippets..." required>
</div>

<button class="main-search-button" type="submit">Search</button>
</div>

</form>


My Answer:
To achieve this, you can modify the form action URL in your Laravel controller before returning the view. Here's an example of how you can do this:

php
public function search(Request $request)
{
$query = $request->input('query');
$token = $request->input('_token');

// Remove _token from the request parameters
$request->request->remove('_token');

// Build the new URL with query parameter first and _token parameter last
$newUrl = url('/search', ['query' => $query, '_token' => $token]);

return redirect($newUrl);
}


In this code snippet, we first retrieve the query and _token parameters from the request. Then, we remove the _token parameter from the request object. Finally, we build a new URL with the query parameter first and the _token parameter last using the `url` helper function and redirect to that URL.

Make sure to adjust the code according to your specific requirements and routes setup in your Laravel application.

Rate this post

4 of 5 based on 6896 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us