Laravel database connect : syntax error or access violation 1071 specified key was too long
Hello all, in this blog I am going to show you database connection with your Laravel project. As I have showed you a simple Laravel app setup by yourself in the previous blog, if you missed that one simply click on this link.
Assume that you have set up your Laravel application and yet to connect it with the database.
I am going to use phpmyadmin as my server, where I will create a database called "laravel_demo_db"
Modify .env file
Now, we will modify our .env file by providing the database, username and password details as following
..
..
DB_DATABASE = laravel_demo_db
DB_USERNAME = root
DB_PASSWORD =
..
..
**Note: As there is no password in my database, I have left the field as blank.
Table Migration
As there is no table in out database, we will run the migrations to create our "users" and "forget_password" tables.
If you are new to "migration" keyword, they are nothing but to create the tables in any databases (to which the current project is connected), and to easily modify and share the application's database schema. Migrations are typically paired with Laravel's schema builder to easily build your application's database schema.
In our project we will find them into database > migrations folder
If you open any of these files, you will probably see 2 functions
1. up() => to create table in our database
2. down() => to delete/drop table from our database
Artisan Migrate
We will run the laravel migrate command in the command prompt. You have to be in the project folder in CMD. In our case it will be "demo" folder as we have created this project by the name of "demo".
Type => php artisan migrate
This will generate 2 tables in our database
1. password_resets
2. users
Yay! you have successfully created a database, tables. Now you can run the project on http://localhost:8000, where you also can register and login with the registered user.
***If you find an error in command prompt/terminal stating that "Syntax error or access violation 1071 specified key was too long...", then you have to follow the following steps.
Solving Error : Syntax error or access violation 1071 specified key was too long...
open AppServiceProvider.php file in any text editors. You will file the file in App > Providers > AppServiceProvider.php
Add the following codes
1. use Illuminate\Support\Facades\Schema; /*Import Schema*/
2. Schema::defaultStringLength(191); /*Increasing StringLength*/
Go to database and delete the migration and the users table to make the database empty and to run the migrate command once again, otherwise it will not generate the tables as it will state that "tables are already exists"
Run => php artisan migrate
Now, if you look into laravel_demo_db database
Run the server once again and it will ready to register!
You will probably able to see the home page after login
If you look into users table now, you will have a registered user
That is it. A basic database connection with you Laravel app have implemented. Hope you liked it a lot. Please share to make others understand.
Thank you!
Add the following codes
1. use Illuminate\Support\Facades\Schema; /*Import Schema*/
2. Schema::defaultStringLength(191); /*Increasing StringLength*/
Go to database and delete the migration and the users table to make the database empty and to run the migrate command once again, otherwise it will not generate the tables as it will state that "tables are already exists"
Run => php artisan migrate
Now, if you look into laravel_demo_db database
Run the server once again and it will ready to register!
You will probably able to see the home page after login
If you look into users table now, you will have a registered user
That is it. A basic database connection with you Laravel app have implemented. Hope you liked it a lot. Please share to make others understand.
Thank you!
Post a Comment