Laravel中创建Model

php中文网
发布: 2016-06-23 13:33:26
原创
1378人浏览过

以前使用的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');    }}
登录后复制

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

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号