CONNECT LARAVEL PROJECT TO A MYSQL DATABASE AND RUN A DATABASE MIGRATION
ฝัง
- เผยแพร่เมื่อ 8 ต.ค. 2024
- Topic: how to connect laravel to a mysql database and run a migration
Important notes to consider:
1. a laravel project already installed/created at c:/xampp/htdocs.
Steps:
1 open xampp - installed and working
2 open vscode - open current laravel project installed/created
3 on terminal - run the current laravel project to check if its working
run php artisan serve
4 on terminal - create a model with migration file
run php artisan make:model products -m
it will create a model called products and a migration file for us
it will also create a migration file for us located at database/migrations named :2023_02_27_133045_create_products_table
5 create a database at localhost/phpmyadmin
open a tab and type in localhost/phpmyadmin at the URL
i created a database called myapp02272023
6 go back to vscode and open .env file
look for DB_DATABASE and change the value with myapp02272023
this will allow our laravel project to connect to the database we created
7 open the migration file and add some important fields for the table
on the schema::create, after the id()
add the desired fields you want to add for the table products
we added the fields for our table
8 - let's run migration
on the terminal run php artisan migrate
all migration files have been successfully installed
9 - go back to the browser - localhost/phpmyadmin to check the database
we should get a list of tables being added already..
we have lists of tables showing after we clicked the database we created.