How to use a Laravel variable in Jquery?

July 14, 2021
Jquery Laravel
How to use a Laravel variable in Jquery?

In this post, you will learn how to use a Laravel variable in Jquery.

The best way is that you declare the variable in jquery/javascript in the head section and then use that.

The solution is also suitable if you have a separate file for the jquery code.

Here is the example:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>

    <script>
        let app_name='{{env('APP_NAME')}}';

    </script>
</head>
<body>

<script>
        console.log(app_name);
</script>
</body>
</html>

In the code above, I have created the variable app_name in the head in which we are loading the Laravel variable env('APP_NAME') and then we can use the app_name to get the value anywhere in the jquery code.

Conclusion:

Hope you have learned how to use a Laravel variable in Jquery.

Let me know if you have any queries in the comments.

See you next time 🙂

Tags

Write a Reply or Comment

Your email address will not be published. Required fields are marked *


Icon