Troubleshooting Laravel's database connection error

61 views Asked by At

I am a novice with Laravel and I'm attempting to establish a connection to my MySQL database. I've created a fresh Laravel installation and when I configure the database values in my .env and database.php files, I consistently encounter an error on the website that displays the following message.

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'practicelaraveldb.sessions' doesn't exist

Then it prompts me to run the migrate command php artisan migrate, and I do, but then I get the error:

Illuminate\Database\QueryException SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (Connection: mysql, SQL: alter table users add unique users_email_unique(email))

  at vendor\laravel\framework\src\Illuminate\Database\Connection.php:813
    809▕                     $this->getName(), $query, $this->prepareBindings($bindings), $e
    810▕                 );
    811▕             }
    812▕ 
  ➜ 813▕             throw new QueryException(
    814▕                 $this->getName(), $query, $this->prepareBindings($bindings), $e
    815▕             );
    816▕         }
    817▕     }

I followed the above error's comments and changed the email field in the user's table, and I am still getting errors. Is it really this complicated trying to get a simple database connection? I'm sure I must be missing something here. I followed the directions in the documentation and some YouTube videos.

https://laravel.com/docs/11.x#databases-and-migrations

1

There are 1 answers

0
Abinash Bhatta On

In the App\Providers\AppServiceProvider.php add the following code.

Import Necessary Classes: Ensure that you have imported the necessary classes at the top of the file: use Illuminate\Support\Facades\Schema;

Modify the boot() Method: Add the following line inside the boot() method:

public function boot()
{
    Schema::defaultStringLength(191);
}