You are citing all basic language syntax rules. A simple google should suffice. Developers should strive to understand the syntax of the language they are using. Also Token Mismatch errors should be banned from this site. In short learn to Google, support Alfred and Read The Manual. Is this a Laracasts Site or Homework Helper?
A List Of Most Common Laravel Errors / Exceptions
Hey there!
I see a lot of people get the same errors over and over again so I thought "why not create a list of the most common errors / exceptions people see every day when developing apps with Laravel?".
I'll start the list with a few of them:
-
Class "User" not found- this means that you haven't told Laravel where to look for the class. In other words, it's looking in the wrong places (usually in the same folder/namespace where the class is called).
To fix this error, you have to either:
- Write the
use Full\Namespace\To\Class;(Exampleuse App\User;) at the top of the file (also known asimport classin PHPStorm when you click on the yellow light bulb). - Tell Laravel to look for it globally by adding a front slash like
\Class::method()(Example:$users = \User::all()) - Write the full namespace of the class where you use the class like
Full\Namespace\To\Class::method()(Example:$users = App\User::all()). - If the class you just added is outside of the autoload path (the app folder, which is autoloaded by default with PSR-4), you have to run
composer dump-autoload -oto load the newly added class.
Feel free to add more options by replying. I'll add everything to the list.
-
NotFoundHttpException in RouteCollection.php line 143- This usually happens when a route you tried to access does not exist or you misspelled it either in your routes.php or in your browser's address bar. -
ModelNotFoundException- This exception is thrown by eitherfindOrFail()orfirstOrFail()methods on the model (User::firstOrFail()orUser::findOrFail()). This means that the ID you are trying to get does not exist in the database. -
MethodNotAllowedHttpException- Usually happens when you try to use an HTTP method for a route but you have not defined that method in your routes file. For example, if you POST to a method, but you only define a GET method for the route in routes.php. -
TokenMismatchException- This exception is usually thrown when using forms (posting a form to a URL). To fix this problem, you have to:
- If you are using a normal form, add a
{!! csrf_field() !!}to your form. (Form::open() and Form::model() automatically generate this field if you are using the illuminate/html or laravelcollective/html (recommended) packages) - If you are using JavaScript/AJAX/JQuery, you have to do two things:
- Add
<meta name="csrf-token" content="{!! csrf_token() !!}">inside the tag of your master / layout page. - Add the following code at top of your JS file (or in a separate file which is loaded above the file(s) you are doing the AJAX call):
- Add
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
You can read about CSRF protection in the documentation.
Feel free to add more of these errors / exceptions and some explanation when and how to fix them. I'll update this list later. Also, feel free to add or correct the current list.
Thanks!
Please or to participate in this conversation.