Laravel和数据库设计

I asked this question on Reddit, but received no answer. I thought I would try here, instead.

I am new to Laravel, and have been going over the documentation and also watching Jeffrey Way's NetTuts videos. However I haven't gotten in too deep just yet, so if this is answered clearly somewhere else, please just point me in that direction.

I come from a CodeIgniter background, and for all projects I have done with it, I typically design my databases in MySQL Workbench. I also use this to make changes to the schema, and also as a visual map of the database. The MySQL Workbench file generally gets passed around with the other developers via Git.

Laravel seems to want you to create your tables using migrations, which seems a bit counter intuitive coming from the MySQL Workbench side. I understand that migrations act as a version control for the database, which seems pretty nice. However, other than that, I don't quite get the point just yet.

Can anyone explain to me why I should be building the tables out via the migrations feature of Laravel vs. the way I've been doing it?

Laravel is a PHP framework. Like other frameworks, such as Zend, your development to deployment timeframe will be significantly reduced, as well as developing within a framework that can be understood by other developers as your projects becomes larger and you need more developers involved in the future.

Migrations, as part of Laravel, are designed to quickly and easily setup a database scheme without the need to type any MySQL. It also presents the schema correctly. As your schema exists within a file, you can easily rollback and transport your schema with ease.

Well, this is a valid concern. While Laravel doesn't specifically force you to use it, I can think of a few reasons for its implementation:

  1. Compatibility. It works with almost any database. If you ever decide to change from MySQL to PostgreSQL, there's very little to change.
  2. Version control. As you mentioned yourself, it allows you to have control over what's changed in your database, and provides quick and easy way to update your already running database.
  3. Easy of use. It's incredibly easy to call it through the command line. Meaning you won't have any problems creating your database on the server side, through a shell.

The main reason Laravel has migrations is because version control. When you create a migration the name has a date attached to it. I use Laravel myself and I absolutely love migrations. Teamed up with the Schema builder it makes my life so much easier not using ancient MySQL methods. Here's some examples of the Schema and migration

Simple Command To Create A New Migration File:

php artisan migrate:make create_users_table

Inside The Users Table Migration File:

Schema::create('users', function($table){
     $table->increments('id'); [will create a increments field with name of ID]
     $table->string('username'); [string field with name of username]
     $table->string('password'); [password is a special  field] 
     $table->text('body'); [string field with name of username]
}); 

At the bottom all you need to add is this:

Schema::drop('users');

Then you run the migrate command:

php artisan migrate

It will add all the columns to the table.

if you ever want to take back or add to the database all you need to do is run this

php artisan migrate:rollback

[you can add specific commands here to only rollback certain tables]