Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm totally new to this type of framework. I've come from barebones PHP development and I can't seem to find an easy to understand guide what migrations actually do.

I'm trying to create a project that already has an existing database. I've used this: https://github.com/Xethron/migrations-generator[1] but making changes to the schema via the migrations seems to spit out errors which means I have no idea what I'm doing.

I really need a simple run down of what migrations actually do, how they affect the database and anything else you think would help an absolute beginner.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
361 views
Welcome To Ask or Share your Answers For Others

1 Answer

Migrations are a type of version control for your database. They allow a team to modify the database schema and stay up to date on the current schema state. Migrations are typically paired with the Schema Builder to easily manage your application's schema.

With migrations you don't need to create table in phpMyAdmin, you can do it in Laravel. Here is an example to create a user table:

class CreateUsersTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function(Blueprint $table)
        {
            $table->increments('id'); // autoincrement id field
            $table->string('name');   // string field
            $table->string('lastname');
            $table->string('title');
            $table->string('email')->unique();   // unique string field
            $table->string('password', 60);      // string field with max 60 characters
            $table->boolean('Status')->default(0); // string field with default value 0
            $table->timestamps();

        });
    }


    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('users');
    }

}

I this code we create table with fields like "name", "lastname"... we said in our Laravel code they are string type when migration is done we have complete table in databese with this fields.

Run a migration to create table

To create a migration, you may use the make:migration command on the Artisan CLI (artisan command line interface):

php artisan make:migration create_users_table

or

php artisan make:migration create_users_table --create=users

Run a migration to alter table

When you need to do some changes in database table example: add field vote to user table you can do like this in your Laravel code without touching SQL code

php artisan make:migration add_votes_to_users_table --table=users

Rollback the last migration operation

If you make mistake and did something wrong you can always rollback to return database in previous state.

php artisan migrate:rollback

Rollback all migrations

php artisan migrate:reset

Rollback all migrations and run them all again

php artisan migrate:refresh

php artisan migrate:refresh --seed

One of best advantage of migrations are creating database without touching SQL code. You can make whole database with relationship in PHP code then migrate it into MySQL, PL/SQL, MSSQL or any other database.

Also I recommend the free Laravel 5 fundamental series, in episode 7 you can hear more about migrations.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...