11/1/19

Migration in Laravel | Part - 9



Migrating
-------------------------------------------------

Migrating is just basically a feature of laravel way which helps us create tables in our database basically in auto mode. meaning that we have a class we run a little PHP artisan command tool and that will create the table for us. we all the fields and columns.


how to migration in database

01. go to the your_project_folder->database->migration. out of box laravel has two migrations. these are classes. we have two migration in default in laravel.
1. user migration, 2. password resets migration.

if you open the file (user or password resets), you see two methods(up(). down()). and extending the Migration class. this class creates the table in the database. once we run this using the PHP artisan command tool.



Schema Information in laravel:

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

So if this up() method runs Schema::create() static method also run. we're using the schema class. we create where the first parameter is that the table name, the second parameter is a function a closure function. this closure function has a database convert into an object. ((Blueprint $table) - dependency injunction) Blueprint holds that object.


* increments('id') - Create a new auto-incrementing integer (4-byte) column on the table. (int)

* string('name') - Create a new string column on the table. (Varchar())

* rememberToken() - Adds the `remember_token` column to the table.

* timestamps() - Add nullable creation and update timestamps to the table.

02. go to the phpmyadmin
your project is cms (Example)
PHPMyAdmin - http://cms.test/phpmyadmin/

03. create database
'laravel_cms'

04. open .env file

05. make to update the file (.env)

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_cms
DB_USERNAME=root
DB_PASSWORD=

06. open the terminal

07. type the command

PHP artisan migrate

when successfully finished, you got the message...
The migration table created successfully.
Migrated: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_100000_create_password_resets_table

08. open the phpmyadmin and check its database. the database will be updated.


-----------------------------------------------------


Creating migration and dropping them

Creating migration
----------------------------------------------

type 'PHP artisan' command you can the see PHP artisan command

you see all the 'migrate' commands

1. Make Migration
php artisan make:migration create_posts_table --create="posts"

Message : Created Migration: 2019_08_27_161033_create_posts_table

2. migrate (table) to database
php artisan migrate
Message : Migrated: 2019_08_27_161033_create_posts_table


dropping migration
----------------------------------------------------
To rollback the latest migration "operation", you may use the rollback command

delete (table) the migration
php artisan migrate:rollback
Rolled back: 2019_08_27_161033_create_posts_table


---------------------------------------------------------

Adding columns to existing tables using migrations

1. make migration
php artisan make:migration add_is_admin_column_to_posts_table --table=posts   
Created Migration: 2019_08_27_163430_add_is_admin_column_to_posts_table

2. migrate (table) to database
php artisan migrate
Migrated: 2019_08_27_163430_add_is_admin_column_to_posts_table

delete the migration
php artisan migrate:rollback
Rolled back: 2019_08_27_163430_add_is_admin_column_to_posts_table

-------------------------------------------------------------------

Some more migration commands

*. delete/reset the migrations (all rollback):
PHP artisan migrate: reset

Rolled back: 2019_08_27_163430_add_is_admin_column_to_posts_table
Rolled back: 2019_08_27_161033_create_posts_table
Rolled back: 2014_10_12_100000_create_password_resets_table
Rolled back: 2014_10_12_000000_create_users_table


*. let's migrate everything again:
PHP artisan migrate

Migrated: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_100000_create_password_resets_table
Migrated: 2019_08_27_161033_create_posts_table
Migrated: 2019_08_27_163430_add_is_admin_column_to_posts_table


*. Let's delete migrate and make migration
php artisan migrate:refresh

Rolled back: 2019_08_27_163430_add_is_admin_column_to_posts_table
Rolled back: 2019_08_27_161033_create_posts_table
Rolled back: 2014_10_12_100000_create_password_resets_table
Rolled back: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_100000_create_password_resets_table
Migrated: 2019_08_27_161033_create_posts_table
Migrated: 2019_08_27_163430_add_is_admin_column_to_posts_table



*. migration status
php artisan migrate:status
+------+------------------------------------------------------+
| Ran? | Migration                                            |
+------+------------------------------------------------------+
| Y    | 2014_10_12_000000_create_users_table                 |
| Y    | 2014_10_12_100000_create_password_resets_table       |
| Y    | 2019_08_27_161033_create_posts_table                 |
| Y    | 2019_08_27_163430_add_is_admin_column_to_posts_table |
+------+------------------------------------------------------+


-----------------------------------------------------------

php artisan make:migration create_users_table --create=users

php artisan make:migration add_votes_to_users_table --table=users

-------------
how to create the model?
PHP artisan make: model Address -m
The model created successfully.
Created Migration: 2019_09_21_121011_create_addresses_table


--------------------------------------------
how to migrate to the database?
PHP artisan migrate

No comments:

Post a Comment

About

Hi, I'm Najathi.
I've started entrepreneurial company, Twin Brothers.Inc.
One is self-funded & the other is venture backed. I also send a weekly. where I share relevent, curated links.

Every Week I Publish a short post on writing, publishing, or content of IT Related

Contact Form

Name

Email *

Message *