1

I'm trying to access this laravel URL inside Javascript:

var id = 2;
var href = '{{ URL::to('/bus/'+ id +'') }}';

I tried to escape the single quote and \ before it, like this:

var id = 2;
var href = '{{ URL::to(\'/bus/'+ id +'') }}';

It's giving me this error:

Parse error: syntax error, unexpected ''/bus/'' (T_CONSTANT_ENCAPSED_STRING), expecting identifier (T_STRING)

1 Answer 1

3

Aside from the mis-matched quotes, id is a client-side JS variable which is not available in your Laravel logic which runs on the server. To fix this get the URL to /bus on the server, then append the id to it on the client. Something like this:

var id = 2;
var href = '{{ URL::to('/bus/') }}' + id;

Note that you may need to append a / between the Laravel output and the id value, it just depends on how the routes are generated.

Sign up to request clarification or add additional context in comments.

2 Comments

when i hit hhref it showing http://localhost/BSProject/public/bus3 instead of http://localhost/BSProject/public/bus/3
In that case, as I mentioned, you need to include a / between the values: var href = '{{ URL::to('/bus') }}/' + id;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.