First, fix bug. Then, love Laravel.

Image for post
Image for post

Bugs during development are indispensable for every programmer, it’s like a “close friend” indispensable in the life of us life.

1. No supported encrypter found

# Problem
RuntimeException in EncryptionServiceProvider.php line 29:
No supported encrypter found. The cipher and / or key length are invalid
orWhoops, looks like something went wrong.# Solution: Run cmdphp artisan key:generate

2. Database connection

# Problem
Unexpected Exception: SQLSTATE[HY000] [1045] Access denied for user ****@'localhost' (using password: YES)
# Solution: Check connection DB_USERNAME, DB_PASSWORD in file .env or config/database.php

3. Reflection Exception

# Problem
[ReflectionException]
Class LoveLaravelSeeder does not exist
# Solution: Run cmd
composer dump-autoload

4. Laravel migration

# Problem
[Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
# Solution: Edit file AppServiceProvider.php
use Illuminate\Support\Facades\Schema;
.....
public function boot()
{
Schema::defaultStringLength(191);
}

5. Verify Csrf Token

# Problem
TokenMismatchException in VerifyCsrfToken.php line 68:
# Solution: Add method in <form>...</form>{{ csrf_field() }}When use ajax(jquery):$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});

6. Folder permission

# Problem
NotFoundHttpException in RouteCollection.php
# Solution: This error may be due to set permission
sudo chmod -R 777 storage
sudo chmod -R 777 bootstrap/cache

7. The N+1 Problem

# Problem
This is not an error, but it implies a lot of risk.
# Solution: Eager loading
https://laravel.com/docs/5.6/eloquent-relationships#eager-loading

8. Truncate tables with foreign key constraints

# Problem
Syntax error or access violation: 1701 Cannot truncate a table referenced in a foreign key constraint ...
# Solution:class DatabaseSeeder extends Seeder {
public function run()
{
Eloquent::unguard();
//disable foreign key before running seeders
DB::statement('SET FOREIGN_KEY_CHECKS=0;');
$this->call('UsersTableSeeder');
...
DB::statement('SET FOREIGN_KEY_CHECKS=1;');
}
}

9. Failed opening required bootstrap/../vendor/autoload.php

# Problem
[RuntimeException]
Error Output: PHP Warning: require(bootstrap/../vendor/autoload.php):
# Solution:Run composer with --no-scripts
composer update --no-scripts

10. Hide .env passwords when whoops output

# Problem
It is scary if the implementation has an error, and everything in the .env file is revealed :(
# Solution: Given this in config/app.php filereturn [
'blacklist' => [
'_ENV' => [
'APP_KEY',
'DB_PASSWORD',
'REDIS_PASSWORD',
'MAIL_PASSWORD',
'PUSHER_APP_KEY',
'PUSHER_APP_SECRET',
],
'_SERVER' => [
'APP_KEY',
'DB_PASSWORD',
'REDIS_PASSWORD',
'MAIL_PASSWORD',
'PUSHER_APP_KEY',
'PUSHER_APP_SECRET',
],
'_POST' => [
'password',
],
],
];

Thank you for reading, I hope it will help you in the process of practicing with Laravel.
I ❤️ Laravel.

Written by

Be Curious!| ☕️+✍🏼=❤️ | buihuycuong.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store