0

I'm using Laravel's var ({{ $test }}), assets ({{ asset('upload/img/something.png' }}), route ( {{ route('something.something') }} ) in a js script. This script works correctly if its in a blade file.

I want to has this script inside a js file. I know that I can create a js function and call this in a blade file with arguments, but I have no more arguments. How I can solve my problem?

2
  • please add the code you have tried Commented Aug 3, 2019 at 9:33
  • 1
    Make an object of your arguments and send the object as the single argument. Commented Aug 3, 2019 at 9:41

1 Answer 1

2

You can declare your variables in blade file, then include the javascript file to use them.

Example: In your blade file you declare a url variable:

<script>
   var url = "{{ route('something.something') }}";
</script>

Notice that we use var so that the variable will be visible in the scripts below this.

Then you include your script file:

<script src="{{ asset('js/myScript.js' }}"></script>

In that file you can use the url variable.

PS: maybe if you provide more code I will be able to help you more.

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

Comments

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.