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

mir86's avatar
Level 1

Laravel localization

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.

0 likes
17 replies
mir86's avatar
Level 1

@vincent15000 don t want to change it but pass the value to session local (lang)

p.s i will look into your option for further solution ,looks great, still ,want to understand my error :)

1 like
vincent15000's avatar

@mir86 What do you see in the console if you type this ?

<script type="text/javascript">
    var url = "{{ route('changeLang') }}";

	console.log(url);

    $(".changeLang").change(function(){
        window.location.href = url + "?lang="+ $(this).val();
    });
</script>
vincent15000's avatar

@mir86

var url = "{{ route('changeLang') }}";

console.log(url);

If the console shows only [HTTP/1.1 200 OK 20ms] after a console.log(url), that means that the url variable is empty.

mir86's avatar
Level 1

@vincent15000 why of i pass the value ?

        <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>

i pass the value to controller get('locale') == 'fr' then i check with the curr value ? 'selected'

1 like
vincent15000's avatar

@mir86 Sorry from the beginning I should have noticed that.

<a href {{ session()->get('locale') == 'en' ? 'selected' : '' }}>English<a/>

You don't set any value to the href attribute and it can't work just like this.

You should have something like href="...", but you don't have any.

I understand that you have this code and that you want to perfect it, but according to me it's really not a good pratice to do like this.

mir86's avatar
Level 1

@vincent15000 tried already this :

  <a href="{{ route('LangController.changeLang', ['lang' => 'en']) }}" >eng</a>
  <a href="{{ route('LangController.changeLang', ['lang' => 'fr']) }}" >fr</a>

not working either ;(
1 like
mir86's avatar
Level 1

@vincent15000

Route::get('/', function () {
    return view('welcome');
});

Route::get('/', [LangController::class, 'index']);
Route::get('lang/change', [LangController::class, 'change'])->name('changeLang');
1 like
mir86's avatar
Level 1

@vincent15000 Well syntax is correct ,but ,i need to pass the value to session ,not to the route ,to change the session.locale.

In your soluion it will pass it as route parameter ,not session ... still not sure ...

1 like
vincent15000's avatar
Level 63

@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]);
}
1 like
mir86's avatar
Level 1

@vincent15000

!The only way to pass datas from JS to PHP is using an HTTP request.!

That is the error i dod not considered ! That is root of my logic error ! I do thank you Vincent !

Now works just perfect

1 like

Please or to participate in this conversation.