Laravel

5 common errors in laravel which haunt new developers

Errors and exceptions are integral parts of software development. In this post I discussed major mistakes by new developers and they get bewildered.

5 common errors in laravel which haunt new developers

Hello readers ! hope you are doing fine and enjoying my blog. Laravel is beautiful, no doubt, but its error reporting while in development environment is too much haunted. As far as I know everyone has seen a black and white screen with millions of characters arranged line by line, even if there is a semicolon (;) missing.

Our today’s topic would be the top 5 common errors / exceptions or simply the mistakes new developers make while working on Laravel which are:

  1. MethodNotAllowedHttpException
  2. 419 Error / Page Expired
  3. File Permission for storage
  4. ReflectionException / Class does not exist]
  5. 500 Error

The five errors, and how to fix each

  1. MethodNotAllowedHttpException. The route exists, but not for the HTTP verb you used: a form posting to a route declared with Route::get, or a GET link to a Route::post. Run php artisan route:list, match the verb, and remember that HTML forms can only send GET and POST; use @method('PUT') or DELETE inside the form for the rest.
  2. 419 Page Expired. Laravel rejected the request because the CSRF token was missing or the session had expired. Every POST form needs @csrf; AJAX requests need the token in the X-CSRF-TOKEN header. If it happens only on the live server, the session driver or the APP_URL/cookie domain is usually wrong.
  3. Storage permission errors. "The stream or file could not be opened" means the web server cannot write to storage/ or bootstrap/cache/. Give ownership to the web user and write permission to those two folders only, never chmod 777 the whole project.
  4. ReflectionException: Class does not exist. A class was renamed or moved and the autoloader does not know. Check the namespace matches the folder, then run composer dump-autoload; for a controller referenced in a route, check the import at the top of routes/web.php.
  5. 500 Server Error with no detail. On a live server APP_DEBUG=false hides the message on purpose. Read storage/logs/laravel.log instead; nine times out of ten it is a missing .env value, a wrong database credential or a PHP version below the one the project requires.

Run php artisan config:clear and php artisan cache:clear after fixing any of these; a stale cached config reproduces an error you have already solved.

Written by

Tech BD

Editorial team of Tech BD.