天天看點

php artisan建立model,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中的代碼: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('是否是預設位址'); }); } public function down() { Schema::drop('addresses'); }}

4、執行:php artisan migrate 指令在資料庫中生成表User_address。

本文原創釋出php中文網,轉載請注明出處,感謝您的尊重!