Step 1: Create a Database
In order for your Laravel site to work locally, you will likely need to connect to a MySQL database in order to run migrations, access and store data, etc. You can either connect to a remote database or create a database locally.
To connect to a remote database, skip to Step 3.
In order to create a database locally, you need to add a database to your local environment. You can do that by running the following SQL command below in PHPMyAdmin, in a MySQL client like Sequel Pro, or via teh command line (after connecting to the database using mysql -u username -p):
CREATE DATABASE database_name;
Step 2: Add MySQL User that Can Access that Database
You need to create a user, grant all privileges on the newly created database to that user, and then flush privileges to make sure those newly added privileges work. you can do this by running the three statements in PHPMyAdmin, a MySQL client, or via the command line.
CREATE USER 'username'@'%' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'%'; FLUSH PRIVILEGES;
Step 3: Create and Update the Laravel Project’s .env file
Make sure you use the database credentials that are associated with the user that you just created, the database that you just created, and make sure that the a that exists on your local development environment. So, if you use the credentials specified above on your local environment the following .env database variables will look like the below:
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=database_name DB_USERNAME=username DB_PASSWORD='password'
You may need to set your DB_HOST to localhost as well depending on your local development environment.
If you are using a remote database, you would use the remote database’s IP address as the DB_HOST and use the remote database name and database username and password (DB_DATABASE, DB_USERNAME, DB_PASSWORD).
Step 4: Run php artisan key:generate
If you just created a database locally, you need to generate a key using php artisan. You need to navigate to the root fo your Laravel install and run the following command.
php artisan key:generate
If you are connecting to a remote database the key needs to be the same key that is currently being used to connect to that database.
Step 5: Run composer install
In order to setup Laravel locally, you are going to need to run the following command to generate the packages necessary for Laravel and the functionality that you enable in your site to work properly. This command will generate a vendor directory that includes all of the packages that you specify in your composer.json file. Make sure to navigate to the root of your Laravel install where the composer.json file is located before running this command.
Step 6: Run php artisan migrate
You’re almost there, but if you created any migrations in your Laravel project you need to use the php artisan migrate command to create the database structure that you require for your Laravel Project.
php artisan migrate
There are several additional steps that you may need. One of these steps includes running php artisan db:seed to add data to your database that you need for your application to function properly.