How to resolve the $ is not defined error in Laravel
Thank you for your continued support.
This article contains advertisements that help fund our operations.
⇨ Click here for the table of contents of Laravel articles
This article explains how to resolve the $ is not defined error in Laravel.
What is $?
$ is a function that can be used in jQuery.
Why does this error occur?
$ is not defined
Translated directly, it means "$ is not defined."
This means that jQuery is not in a usable state.
Solution (the easiest way)
Basically, you just need to make sure jQuery is usable.
Towards the bottom of the head tag, write the following line of script.
The place where this head tag is written may vary from person to person, but in most cases, it is likely to be in resources/views/layouts/app.blade.php.
<head>
<!-- There are various tags here -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<!-- There are various tags here -->
</head>
Points to note
If the script you added is not read first, it may result in an error stating that $ is not defined.
In other words,
<head>
<!-- There are various tags here -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<script>
$("#app").html(
"Make sure to write scripts using the $ syntax after loading the jQuery script below"
)
</script>
<!-- There are various tags here -->
</head>
Make sure to use syntax like $ after loading jQuery.
Summary
That's it.
I hope this can be helpful to someone.
For feedback or complaints, please contact me via Twitter DM.
Until next time!
Popular Articles
Deploying a PHP7.4 + Laravel6 project to AWS EC2
Implementing breadcrumbs in Laravel using laravel-breadcrumbs