How To Setup MySQL in Laravel
Laravel is a popular PHP framework that makes it easy to work with MySQL databases. Here’s a simple explanation of how to work with MySQL in Laravel:
Before jumping into the tutorial, make sure you already installed the Laravel project. If you have already set up all the required components, you can start connect the MySQL to Laravel.
- Suppose you’re using VSCode, run
code .
to open the project folder. - Open Browser if using xammp or wamp64.
- Open .env File:
Type
localhost/phpmyadmin
, create a database, and remember the database name, as it is a crucial part of MySQL configuration. For example, if you create a database namedtesting
, make sure to use it correctly in your application setup.
Open Browser if using xammp or wamp64.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=testing
DB_USERNAME=root
DB_PASSWORD=""
If you are using Docker or another configuration for your database, make sure to configure it accordingly. The database name, username, and password are important. If you are using XAMPP or WAMP, follow the instructions mentioned above.
After everything is set up, if you want to make additional changes in the migration, you can do so by going to project_name/database/
. Here, you can modify the migration file as needed and then run the migration again to apply the changes.
migrations/user_table.php
Schema::create(‘users’, function (Blueprint $table) {
$table->id();
$table->string(‘name’);
$table->string(’email’)->unique();
$table->string(‘password’);
$table->boolean(‘is_verified’)->default(0);
$table->timestamps();
});
Now, after this, run php artisan migrate
and all the tables will be set up in the database. You can also check them on localhost/phpmyadmin
.
This is it!!! Your Database is setup successfully and ready to build something interesting and Happy Coding!