Hmmm ... it seems that you want to change the URL with JS ?
Why not use a package that handles internationalization for you ?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Greetings everyone ,
I want to change pass a parameter value via the a href value in my blade view (from dropdown form to a href value ) page like that:
my blade view code:
<div class="col-md-4">
<a href {{ session()->get('locale') == 'en' ? 'selected' : '' }}>English<a/>
<a href {{ session()->get('locale') == 'fr' ? 'selected' : '' }}>France</a>
<a href {{ session()->get('locale') == 'sp' ? 'selected' : '' }}>Spanish</a>
</div>
here is my js part that get the value from the view page:
<script type="text/javascript">
var url = "{{ route('changeLang') }}";
$(".changeLang").change(function(){
window.location.href = url + "?lang="+ $(this).val();
});
</script>
here is the LangController that change the value of the session locale :
class LangController extends Controller
{
/**Display a listing of the resource.*@return \Illuminate\Http\Response */
public function index()
{
return view('lang');
}
/*** Display a listing of the resource.*@return \Illuminate\Http\Response */
public function change(Request $request)
{
App::setLocale($request->lang);
session()->put('locale', $request->lang);
return redirect()->back();
}
}
So want the idea is ,is that clicking on the English link on my view page i want to pass that '## 'parameter value to the LangController ,but i made a syntax/logic error in my view code part i guess, if anyone can advise ;)
Thx.
@mir86 Ok but this can't work because the route doesn't exist.
route('LangController.changeLang')
You don't have any route with this name.
What I understand now is that you want to pass the value to the session when you click on a link. The only way to pass datas from JS to PHP is using an HTTP request.
By using this link : <a href="{{ route('changeLang', ['lang' => 'en']) }}" >eng</a>, you will pass the lang parameter to the backend and then you can put it in the session.
In your LangController controller
public function change(Request $request)
{
session(['locale' => $request->lang]);
}
Please or to participate in this conversation.