Home > Backend Development > PHP Tutorial > Laravel中创建Model

Laravel中创建Model

WBOY
Release: 2016-06-23 13:33:26
Original
1245 people have browsed it

以前使用的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中的代码:<?phpuse 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');    }}
Copy after login

4、执行:php artisan migrate 命令在数据库中生成表User_address。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template