Learning Objectives: Understanding the use of MySQL Database using Laravel Migration. We will learn the process of creating a table using Migration in Laravel.
Step 1: MySQL Database Connection Settings
Goto .env file and set up your MySQL database name. First go to PHPMyAdmin and create a new database name as: db1 then in .the env file update the database name to db1
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=db1 DB_USERNAME=root DB_PASSWORD=
Now it is time to run the command and set the database Cache
php artisan config:Cache
Now it is time to run Laravel migration command:
php artisan migrate
Now let’s create a new migration page and we can create a new table in our database. the following command will create a new file with names as products. In this file, you can multiple fields as per your requirements. After you write down all the fields you need to run mthe igrate command to update the table. Reference
php artisan make:migration create_flights_table
Have you done adding new fields as follows into products:
public function up() { Schema::create('products', function (Blueprint $table) { $table->id(); $table->string("name"); $table->string("address"); $table->timestamps(); }); }
php artisan migrate
if you want to reverse migration then you can use rollback command
php artisan migrate:rollback
How many column types are available? Here are the list for your reference: Reference
bigIncrements
bigInteger
binary
boolean
char
dateTimeTz
dateTime
date
decimal
double
enum
float
foreignId
foreignIdFor
foreignUlid
foreignUuid
geography
geometry
id
increments
integer
ipAddress
json
jsonb
longText
macAddress
mediumIncrements
mediumInteger
mediumText
morphs
nullableMorphs
nullableTimestamps
nullableUlidMorphs
nullableUuidMorphs
rememberToken
set
smallIncrements
smallInteger
softDeletesTz
softDeletes
string
text
timeTz
time
timestampTz
timestamp
timestampsTz
timestamps
tinyIncrements
tinyInteger
tinyText
unsignedBigInteger
unsignedInteger
unsignedMediumInteger
unsignedSmallInteger
unsignedTinyInteger
ulidMorphs
uuidMorphs
ulid
uuid
vector
year