0

I have simple javascript code:

var photo = data.photo;
console.log("{{url('" + photo + "')}}");

Here I use laravel url() method. But I can't display value photo inside laravel method. How I can display it?

7
  • use console.log(photo) Commented Feb 7, 2018 at 15:27
  • I must get full url with photo var value @Sohel0415 Commented Feb 7, 2018 at 15:27
  • It's the server which transforms templates like {{url(stuff)}} into http://someurl, but this code does not run on the server, it's all client-side. Commented Feb 7, 2018 at 15:27
  • For example http://domain.loc/uploads/photo.jpg Commented Feb 7, 2018 at 15:28
  • So, I can't display using url() javascript var, yes @James ? Commented Feb 7, 2018 at 15:30

1 Answer 1

3

It's a bad practice to mix Blade syntax with JS. You should do something like this in one of Blade templates:

<script>
    let window.url = {{ url('/') }}
</script>

Then in JS files use this variable:

var photo = data.photo;
console.log(window.url + photo);

You'll find another related code example in my Laravel best practices repo.

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

7 Comments

Thanks, @Alexey Mezenin! I'll see to your Laravel best practices book.
Alexey what a good practice for get url to javascriprt var in blade?
I must create hidden input or div's and get value from HTML code?
Or I can use in javascript file var siteurl = "{{url('/')}}"; code?
I think what using {{ url('/') }} in javascript code will be not work.
|

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.