Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

davorminchorov's avatar

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:

  1. Write the use Full\Namespace\To\Class; (Example use App\User;) at the top of the file (also known as import class in PHPStorm when you click on the yellow light bulb).
  2. Tell Laravel to look for it globally by adding a front slash like \Class::method() (Example: $users = \User::all())
  3. Write the full namespace of the class where you use the class like Full\Namespace\To\Class::method() (Example: $users = App\User::all()).
  4. 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 -o to 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 either findOrFail() or firstOrFail() methods on the model ( User::firstOrFail() or User::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:

  1. 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)
  2. 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):
$.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!

0 likes
5 replies
jimmck's avatar

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?

1 like
zachleigh's avatar

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.

davorminchorov's avatar

@jimmck I understand your point of view, let them figure out the basics themselves (reading articles / watching videos), which will help them in the long run. I am all in for that, but on the other side, there are new people who are trying to learn Laravel and even programming and they might need someone to help them out somehow. They don't know that these exceptions are part of the basics of Laravel, they just wanna build something and learn new stuff.

It's really hard to figure out what way to go, the "help them every step of the way" or "let them figure it out on their own" way. (Good Cop, Bad Cop scenario) If you are harsh with them, they'll think that you are not really a helpful person until they find out that you were harsh to them for a reason, and that reason being "You have to figure out stuff on your own and learn it by yourself the hard way, which is harsh but it will help you in the long run".

We are talking about different types of people here. Some people might understand it, but some might not, so it really depends on the situation. Both ways are right in the right situation.

Oh, and thanks for the TokenMismatchException reminder :)

1 like
jimmck's avatar

@Ruffles Hey I Hear You and I know I sound Pissy. I always read your posts for Info!. And I do chip in and answer. But of late sifting through endless posts, of the same ole same ole. I subscribe to get Laravel information and see what other Laravel developers are doing and yes facing. It would be great if the subscribers could filter out the noise at times.

1 like
robgeorgeuk's avatar

Great topic. @jimmck I feel your pain too and sometimes I eyeroll when I see another CSRF post but then I remember that everyone has to start somewhere and learning to program is really hard.

Laracasts aims to educate and does it in a very non-condescending way and I think the Laravel community (on the Forum, blogs, Twittter etc) have a responsibility to be kind and gently steer people in the right direction regardless of whether a quick Google search would reveal the answer.

I think @Ruffles idea of a FAQ is a good one as this solves this issue. Perhaps @JeffreyWay could help facilitate this is some way?

Please or to participate in this conversation.