Laravel创建Model

它已被用于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。
这是生成表address的过程

版权声明:本文博客原创文章,博客,未经同意,不得转载。

posted @ 2015-08-21 21:18  hrhguanli  阅读(1075)  评论(0编辑  收藏  举报