Most Common Laravel 5.x Version Errors / Exceptions And Errors Solutions

0
109

Are you developing apps with Laravel or Beginner of Laravel Framework? If yes you must face some common Errors in here.

1) Error Detect:

If Error Show like – Class “User” not found ( wrong places (usually in the same folder/namespace where the class is called )

Solution :

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).

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),

To run

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:

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:

1) Addinside the tag of your master / layout page.
2) 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):

Can Called Individual Error Page for Each Error Display:

Create all pages releted to HTTP status code in following path resources/views/errors/

Here a common list of HTTP status code which you face everytime in application :

400 for Bad Request
403 for Forbidden
404 for Not Found
408 for Request Timeout
408 for Request Timeout
500 for Internal Server Error

You can create a view file like

400.blade.php
403.blade.php
404.blade.php
500.blade.php

for this type of HTTP status code .

Place Error Handler in Laravel

You can make changes in following path app/Exceptions/Handler.php within render method.

Just Change Handler.php file with bellow Code:

You can call Specific Page using Switch Keyword

Now Create View Page for Each Page Error:

Path Directory : resources/views/errors/404.blade.php

404

 


Sorry – Page Not Found!

The page you are looking for was moved, removed, renamed
or might never existed. You stumbled upon a broken link 🙁