数据库配置

Laravel数据库配置在app/config/database.php中进行,其中

  1. 'fetch'=> PDO::FETCH_CLASS,

设置数据返回格式,默认以类放回,你以$book->name形式访问数据。如果设置成PDO::FETCH_ASSOC,则以数组形式返回$book['name']

  1. 'default'=>'mysql',

指定数据库连接,在下面的connections中配置你需要的数据库。你也可以创建你自己的多个数据库连接。

  1. 'connections'=> array(
  2. ......
  3. 'mysql'=> array(
  4. 'driver'=>'mysql',
  5. 'host'=>'localhost',
  6. 'database'=>'database',
  7. 'username'=>'root',
  8. 'password'=>'',
  9. 'charset'=>'utf8',
  10. 'collation'=>'utf8_unicode_ci',
  11. 'prefix'=>'',
  12. ),
  13. ......
  14. ),

Schema结构生成器

创建表

  1. Schema::create('users',function($table)
  2. {
  3. $table->increments('id');
  4. $table->string('username',32);
  5. });

创建了一张users表,包含俩个字段。关于其他类型的字段参考官方手册。

字段修饰

  1. $table->string('username');
  2. $table->unique('username');
  3. //链式
  4. $table->string('username')->unique();

其他修饰还包括

  1. $table->string('name')->nullable();
  2. $table->string('name')->default('John Doe');
  3. $table->integer('age')->unsigned();
  4. $table->integer('age')->index();
  5. $table->string('username')->primary();

为多个字段添加修饰

  1. $table->index(array('age','weight'));

更新表

重命名表

  1. Schema::rename('users','idiots');

Schema::table()更新表,添加字段

  1. Schema::table('example',function($table)
  2. {
  3. $table->string('name');
  4. $table->string('name')->after('email');
  5. });

删除字段

  1. $table->dropColumn('name');
  2. $table->dropColumn(array('name','age'));
  3. $table->dropColumn('name','age');

重命名字段

  1. $table->renameColumn('name','nickname');

删除修饰

  1. $table->dropPrimary(array('name','email'));
  2. $table->dropUnique('example_name_unique');
  3. $table->dropIndex('example_name_index');

删除表

  1. Schema::drop('example');
  2. Schema::dropIfExists('example');

指定数据库连接

  1. Schema::connection('mysql')->create('example',function($table)
  2. {
  3. $table->increments('id');
  4. });

检查存在性

  1. Schema::hasTable('author')
  2. Schema::hasColumn('example','id')

设置存储引擎

  1. $table->engine ='InnoDB';

Migrations

Migrations是一种数据库版本控制工具,其当前数据库保持最新或是返回到过去的某个版本。

创建Migrations

使用 Artisan 命令行的migrate:make命令创建一个Migrations

  1. $ php artisan migrate:make create_users
  2. CreatedMigration:2013_06_30_124846_create_users
  3. Generating optimized class loader
  4. Compiling common classes

会创建一个PHP文件在app/database/migrations/2013_06_30_124846_create_users.php,里面包含两个默认函数up()down().然后我们可以在里面创建表或是删除表

  1. publicfunction up()
  2. {
  3. Schema::create('users',function($table)
  4. {
  5. $table->increments('id');
  6. $table->string('name',128);
  7. $table->string('email');
  8. $table->string('password',60);
  9. $table->timestamps();
  10. });
  11. }
  12. publicfunction down()
  13. {
  14. Schema::drop('users');
  15. }

我们也可以让migrate为我们自动创建表样式

  1. $ php artisan migrate:make create_users --create --table=users

还可以使用--path=app/migs选项指定路径

运行Migrations

Laravel会自动安装Migrations,你也可以手动安装Migrations

  1. $ php artisan migrate:install

运行Migrations

  1. $ php artisan migrate
  2. Migration table created successfully.
  3. Migrated:2013_08_20_044917_create_users

运行时有可能出现[PDOException] SQLSTATE[HY000] [2002] No such file or director错误,那是因为你的mysql.sock设置不正确。还有就是命令行的PHP是系统默认的PHP而不是XAMPP中的PHP,因此你需要修改系统默认的php.ini文件,找到并修改pdo_mysql.default_socket

  1. pdo_mysql.default_socket=/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock

运行某路径下的Migrations

  1. $ php artisan migrate --path=app/migs

运行某个包下的所有迁移

  1. $ php artisan migrate --package=vendor/package

回滚Migrations

回滚最后一次迁移

  1. $ php artisan migrate:rollback

回滚所有迁移

  1. $ php artisan migrate:reset

回滚所有迁移并重新运行所有迁移

  1. $ php artisan migrate:refresh
  2. $ php artisan migrate:refresh --seed

结束