首頁 > php框架 > Laravel > 主體

laravel建立資料表(使用命令列結合程式碼)

藏色散人
發布: 2020-11-26 13:40:26
轉載
3877 人瀏覽過
##上所中由

Laravel#laravel 建立資料表,並希望對需要的朋友有所幫助!

雖然可以直接在資料庫中建立資料表,但是不便於以後專案的遷移。現使用命令列結合程式碼的方式來進行產生。

1、透過指令建立資料表檔案

php artisan make:migration create_table_customers
登入後複製

laravel 创建数据表

#2、在資料表文件中完善資料表相關欄位

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTableCustomers extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create(&#39;customers&#39;, function (Blueprint $table) {
            $table->increments(&#39;id&#39;);
            $table->string(&#39;mobile&#39;)->nullable()->unique();
            $table->string(&#39;email&#39;)->unique();
            $table->string(&#39;website&#39;)->default(&#39;website&#39;)->comment(&#39;站点:applet、website&#39;);
            $table->string(&#39;store_id&#39;)->default(&#39;1&#39;)->comment(&#39;店铺 ID&#39;);
            $table->string(&#39;first_name&#39;);
            $table->string(&#39;last_name&#39;);
            $table->integer(&#39;appellation&#39;)->comment(&#39;称谓&#39;);
            $table->dateTime(&#39;birthday&#39;)->comment(&#39;生日&#39;);
            $table->string(&#39;province&#39;)->comment(&#39;省&#39;);
            $table->string(&#39;city&#39;)->comment(&#39;市&#39;);
            $table->string(&#39;district&#39;)->comment(&#39;区/县&#39;);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists(&#39;customers&#39;);
    }
}
登入後複製

laravel 创建数据表

#3、產生資料表

php artisan migrate
登入後複製

laravel 创建数据表

laravel 创建数据表
此時,資料表已經產生!                                           之後中使用

以上是laravel建立資料表(使用命令列結合程式碼)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:learnku.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!