Skip to content

Instantly share code, notes, and snippets.

@ProcessEight
Last active May 23, 2024 16:55
Show Gist options
  • Save ProcessEight/c00e08a0e8ee46503c6708f0bf8b758b to your computer and use it in GitHub Desktop.
Save ProcessEight/c00e08a0e8ee46503c6708f0bf8b758b to your computer and use it in GitHub Desktop.
Troubleshooting common Laravel problems

Laravel Troubleshooting

Logging

PHP Fatal error: Uncaught Error: Class "Monolog\Logger" not found in ./vendor/monolog/monolog/src/Monolog/Handler/AbstractHandler.php:60

This occurred because the PHP PSR extension php8.3-psr was enabled, but is apparently 'broken' (see Seldaek/monolog#1876 (comment)). Disabling the extension fixed the problem.

If that's not it, then try these steps:

Verify that the Monolog package is installed in ./vendor/monolog/monolog. Then verify that PSR-4 autoloading is correctly configured in composer.json:

{
    "autoload": {
        "psr-4": {
            "Monolog\\": "vendor/monolog/monolog/src/Monolog"
        }
    }
}

Try:

  • Regenerate the autoloader: composer dump-autoload -o
  • Reinstall monolog: composer reinstall monolog/monolog
  • Reinstall Laravel: composer reinstall laravel/laravel
  • Try upgrading or downgrading Monolog

Database migrations

could not find driver (SQL: PRAGMA foreign_keys = ON;)

$ php artisan migrate

   WARN  The SQLite database does not exist: database/database.sqlite.  

  Would you like to create it? (yes/no) [no]
❯ yes


   Illuminate\Database\QueryException 

  could not find driver (SQL: PRAGMA foreign_keys = ON;)

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:760
    756▕         // If an exception occurs when attempting to run a query, we'll format the error
    757▕         // message to include the bindings with SQL, which will make this exception a
    758▕         // lot more helpful to the developer instead of just the database's errors.
    759▕         catch (Exception $e) {
  ➜ 760▕             throw new QueryException(
    761▕                 $query, $this->prepareBindings($bindings), $e
    762▕             );
    763▕         }
    764▕     }

      +41 vendor frames 
  42  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()

Verify you have the PHP SQLite3 driver installed:

$ php -i | grep sqlite
/etc/php/8.1/cli/conf.d/20-pdo_sqlite.ini,
/etc/php/8.1/cli/conf.d/20-sqlite3.ini,
PDO drivers => mysql, sqlite
pdo_sqlite
sqlite3
sqlite3.extension_dir => no value => no value

Email

Connection could not be established with host "mailhog:1025": stream_socket_client(): php_network_getaddresses: getaddrinfo for mailhog failed

Laravel seems to assume you are running in a container out of the box. Therefore, try changing MAIL_HOST=mailhog to MAIL_HOST=0.0.0.0 (or whatever host Mailhog states it is using) in the Laravel .env file.

Models

Call to undefined relationship [user] on model [App\Models\Chirp].

This may happen when trying to use a relationship to retrieve related models, but there is no such configured relationship on the model. For example, trying to get the user related to a chirp:

class ChirpController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('chirps.index', [
            'chirps' => Chirp::with('user')->latest()->get(),
        ]);
    }

Verify that the model Chirp has a method named user which defines the relationship between the Chirp and User models:

class Chirp extends Model
{
    // ...

    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

Validation

Method Illuminate\Validation\Validator::validateMax.255 does not exist.

Make sure the validation method is separated by a colon, not a period (i.e. max:255 rather than max.255):

$validated = $request->validate([
   'message' => 'required|string|max:255',
]);

Blade

Changes don't appear on refresh

  • Try restarting php artisan serve, if you're using that.
  • Try restarting npm run dev, if you're using that
  • Try restarting PHP
  • Try disabling Xdebug
  • Try disabling Opcache

ERROR: npm is known not to run on Node.js v10.8.0

ERROR: npm is known not to run on Node.js v10.8.0
You'll need to upgrade to a newer Node.js version in order to use this
version of npm. You can find the latest version at https://nodejs.org/

Try installing a more recent version of Node using NVM:

nvm install 16.12.0

Then re-install Breeze:

$ composer2 require laravel/breeze --dev
Using version ^1.14 for laravel/breeze
./composer.json has been updated
Running composer update laravel/breeze
Loading composer repositories with package information
Updating dependencies
Nothing to modify in lock file
Installing dependencies from lock file (including require-dev)
Nothing to install, update or remove
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi

   INFO  Discovering packages.  

  laravel/breeze .............................................................................................................................. DONE
  laravel/sail ................................................................................................................................ DONE
  laravel/sanctum ............................................................................................................................. DONE
  laravel/tinker .............................................................................................................................. DONE
  nesbot/carbon ............................................................................................................................... DONE
  nunomaduro/collision ........................................................................................................................ DONE
  nunomaduro/termwind ......................................................................................................................... DONE
  spatie/laravel-ignition ..................................................................................................................... DONE

81 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
> @php artisan vendor:publish --tag=laravel-assets --ansi --force

   INFO  No publishable resources for tag [laravel-assets].

and re-install Blade:

$ php artisan breeze:install blade

added 93 packages, and audited 94 packages in 8s

20 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

> build
> vite build

vite v3.2.3 building for production...
✓ 49 modules transformed.
public/build/manifest.json             0.25 KiB
public/build/assets/app.badde8dd.css   22.30 KiB / gzip: 4.75 KiB
public/build/assets/app.d426e523.js    135.11 KiB / gzip: 49.03 KiB

   INFO  Breeze scaffolding installed successfully.  

Routing

Laravel returns a 404, even though I registered my route in routes/web.php

To diagnose, see if the route returns a 404 when served with artisan serve:

> php74 artisan serve
PHP 7.4.12 (cli) (built: Oct 31 2020 17:03:53) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.12, Copyright (c), by Zend Technologies
    with blackfire v1.44.0~linux-x64-non_zts74, https://blackfire.io, by Blackfire
Starting Laravel development server: http://127.0.0.1:8000
[Sat Nov 21 17:37:10 2020] PHP 7.4.12 Development Server (http://127.0.0.1:8000) started

Now try visiting the 404'ing URL at http://127.0.0.1:8000/my-404ing-route.

If the 404 now works, then it is a server configuration problem.

For Nginx, make sure the following location block exists somewhere inside the server block of your Nginx vhost:

location / {
    try_files $uri $uri/ /index.php?$query_string;
}

See https://laravel.com/docs/8.x#nginx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment