以前使用的CI框架,最近学习使用Laravel框架了,把碰到的一些问题总结一下做个记录,以便以后回顾,也希望可以帮到碰到同样问题的朋友。
在Laravel中数据库表都是根据Laravel中写好的程序去生成的,这样的话便于使用git等版本控制进行管理整个项目。
以建立User_address模型为例进行记录:
1、使用php artisan make:model User_address命令创建模型,如图:
2、成功之后再程序目录app和database/migrations下会分别生成两个文件,如图:
3、打开database/migrations下生成的文件,这个文件就是控制生成数据库表的文件,内容如下:
2015_06_02_071328_create_user_addresses_table.php中的代码:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUserAddressesTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('user_addresses', function(Blueprint $table)
{
$table->increments('address_id')
->comment("主键");
$table->mediumInteger('user_id')
->comment('用户id');
$table->string('consignee', 60)
->comment('收货人');
$table->string('country', 60)
->comment('国家');
$table->string('province', 60)
->comment('省份');
$table->string('city', 60)
->comment('市');
$table->string('district', 120)
->comment('街道');
$table->string('address', 120)
->comment('详细地址');
$table->string('zip_code', 60)
->comment('政编码邮');
$table->string('tel', 60)
->comment('固定电话');
$table->string('mobile', 60)
->comment('手机');
$table->tinyInteger('is_default')
->comment('是否是默认地址');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('addresses');
}
}
4、执行:php artisan migrate 命令在数据库中生成表User_address。
本文详细记录了从传统CI框架转向Laravel框架的过程,特别关注如何利用Laravel自动生成数据库表及其与版本控制系统的整合。通过实例演示了使用`php artisan make:model`命令创建模型,以及在`database/migrations`目录下自动生成的迁移文件如何控制数据库表结构。此外,解释了如何通过`php artisan migrate`命令将这些表部署到数据库中,并讨论了其对项目管理的便利性。

710

被折叠的 条评论
为什么被折叠?



