• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Laravel中创建Model

php 搞代码 3年前 (2022-01-23) 27次浏览 已收录 0个评论

以前使用的CI框架,最近学习使用Laravel框架了,把碰到的一些问题总结一下做个记录,以便以后回顾,也希望可以帮到碰到同样问题的朋友。

在Laravel中数据库表都是根据Laravel中写好的程序去生成的,这样的话便于使用git等版本控制进行管理整个项目。
以建立User_address模型为例进行记录:
1、使用php a(、本文来源gao@!dai!ma.com搞$$代^@码网*搞gaodaima代码rtisan 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。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Laravel中创建Model
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址