在 Linux 下调试 Laravel 错误,常见且实用的方法如下(从基础到进阶):
.envAPP_DEBUG=true
APP_ENV=local
修改后清缓存:
php artisan config:clear
storage/logs/laravel.log
tail -f storage/logs/laravel.log
tail -n 50 storage/logs/laravel.log
dd() / dump()dd($user);
// 或
dump($user);
\Log::error('用户信息', ['user' => $user]);
\Log::info('调试信息');
php artisan route:list
php artisan config:show
php artisan cache:clear
php artisan config:clear
php artisan route:clear
php artisan view:clear
APP_DEBUG=true 时,页面会显示:
| 错误 | 原因 |
|---|---|
| 500 | 代码异常 |
| 404 | 路由不存在 |
| 419 | CSRF 验证失败 |
| 403 | 权限不足 |
DB::enableQueryLog();
// 执行查询
dd(DB::getQueryLog());
或使用:
\Log::info(DB::getQueryLog());
sudo apt install php-xdebug
xdebug.mode=debug
xdebug.start_with_request=yes
配合:
打断点调试
常见错误:
The stream or file could not be opened
解决:
sudo chown -R www-data:www-data storage
sudo chmod -R 775 storage
✅ APP_DEBUG=true
✅ tail -f storage/logs/laravel.log
✅ dd() / Log::error()
✅ php artisan clear 系列命令
如果你有具体报错信息(比如 500、SQL 错误、路由错误),可以直接贴出来,我可以帮你精准分析。