溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

laravel使用命令行結(jié)合代碼創(chuàng)建數(shù)據(jù)表的方法

發(fā)布時(shí)間:2021-01-19 10:12:17 來(lái)源:億速云 閱讀:268 作者:小新 欄目:編程語(yǔ)言

這篇文章主要介紹laravel使用命令行結(jié)合代碼創(chuàng)建數(shù)據(jù)表的方法,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

雖然可以直接在數(shù)據(jù)庫(kù)中創(chuàng)建數(shù)據(jù)表,但是不便于以后項(xiàng)目的遷移。現(xiàn)使用命令行結(jié)合代碼的方式來(lái)進(jìn)行生成。

1、通過(guò)命令創(chuàng)建數(shù)據(jù)表文件
php artisan make:migration create_table_customers

laravel使用命令行結(jié)合代碼創(chuàng)建數(shù)據(jù)表的方法

2、在數(shù)據(jù)表文件中完善數(shù)據(jù)表相關(guān)字段
<?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('customers', function (Blueprint $table) {
            $table->increments('id');
            $table->string('mobile')->nullable()->unique();
            $table->string('email')->unique();
            $table->string('website')->default('website')->comment('站點(diǎn):applet、website');
            $table->string('store_id')->default('1')->comment('店鋪 ID');
            $table->string('first_name');
            $table->string('last_name');
            $table->integer('appellation')->comment('稱謂');
            $table->dateTime('birthday')->comment('生日');
            $table->string('province')->comment('省');
            $table->string('city')->comment('市');
            $table->string('district')->comment('區(qū)/縣');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('customers');
    }
}

laravel使用命令行結(jié)合代碼創(chuàng)建數(shù)據(jù)表的方法

3、生成數(shù)據(jù)表
php artisan migrate

laravel使用命令行結(jié)合代碼創(chuàng)建數(shù)據(jù)表的方法

laravel使用命令行結(jié)合代碼創(chuàng)建數(shù)據(jù)表的方法

以上是“l(fā)aravel使用命令行結(jié)合代碼創(chuàng)建數(shù)據(jù)表的方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI