Learn how to add a database to Laravel. We can use SQLite if we want to. It is connected to Laravel by default. We can use any other database if we want. I will show you how to add MySQL. To add database, our computer will have to install MySQL. I’ll use WAMP server. It has a php, MySQL etc. package in the form of one. There is another useful tool, phpMyAdmin. phpMyAdmin is a great tool.
There is a file for setting database in Laravel, named .env. We have to give our database name, host, password etc in the profile.
Using phpMyAdmin, you can create database and give the information here. You can also use the command line if you want. To create MySQL database using the command line, you need to add the path of mysql to the Windows Environment variable. If you use wamp, the path of mysql will be: C: \ wamp \ bin \ mysql \ mysql6.x.x \ bin
To create database on the command line, first login to mysqal with user name and password. For example, to log in as root user:
mysql -uroot -p
Then to create database:
create database database-name;
If the database will be created in the name given to you.
We can command the query in this database. First of all I want to query that database, I have to select. For him:
mysql use database-name
Then we can use any SQL command. For the basics of SQL, http://www.w3schools.com/sql/
Creating a database using the command line will work but will also work with creating database using phpMyAdmin. An additional feature in phpMyAdmin is that database tables and content are viewed visually and can be edited or added.
Above all, basics of database. Come back to Laravel. Go to our project directory on the command line. Then write the following command
php artisan migrate
If connecting properly on our database, some tables will be added to the database. Go to mysql show tables; [To go to semicolon] or go to phpMyAdmin and see the database.
The command that we have run, is to work on how database database is deployed in the way database tables are created. And what will be the database schemas, it is written in Project> Database> Migrations folder. If we go to this folder, we will see that there are two files inside it. One is for the user table. Another password for the table.
It’s great if everything is right. We were able to connect the database with Laravel!