Problem trying to add route parameter in js

Below is my js code that adds the route and its parameter to the anchor tag.

var href = "{!! route('ShowUserMainForm', ['RoleID'=>" + row.RoleID + "]) !!}"; var UserColumn = "<td><a href=' " + href + "'><button>Users</button></a></td>"; 

It gives below url

http: // localhost: 1234 / public / system-users /% 20 +% 20row.RoleID% 20 +% 20

I expect below.

http: // localhost: 1234 / public / system-users / 1

Did I miss something?

+5
source share
2 answers

No. ways to do this:

Because {!!!!} the block code is converted before JavaScript code, so you need to create a route, and then concat RoleID outside {!!!!}.

if row.RoleID is json:

 var href = "public/system-users/" + row.RoleID 

or

 var href = "{!! route('ShowUserMainForm') !!}" + row.RoleID; 

or

 var href = "{!! route('ShowUserMainForm', ['RoleID'=>'']) !!}" + row.RoleID; 
+8
source

%20 → percent encoding for space character

What you see is the row assigned as the value for RoleID : + row.RoleID + .

You run Laravel inside these external quotes, so just turn off JS string concatenation and write direct PHP (suppose you have a $row variable available on the server side):

 var href = "{!! route('ShowUserMainForm', ['RoleID'=> $row.RoleID]) !!}"; 

When Laravel passes your route into an anchor tag and creates this HTML (everything that happens inside these {!! !!} ), you are working on the server. When you use javascript, you are in the client. You cannot send data from the client to the server the way you are trying.

+1
source

Source: https://habr.com/ru/post/1269357/


All Articles