亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Laravel 創建具有相同列名的表

Laravel 創建具有相同列名的表

PHP
森欄 2023-04-15 17:47:11
我嘗試創建 2 個表:Products - name - description - price - source - processing_started_at - processing_ended_at和Orders - customer_id - order_lines_id - source - processing_started_at - processing_ended_at如您所見,兩個表都有以下列:source、processing_started_at和processing_ended_at將來我想創建更多具有相同列名的表,也許我會創建所有表都需要的更多列。所以我想知道最好的方法是讓事情保持干凈一點。每當我創建新的遷移文件時,有沒有辦法創建某種默認列?還是我應該建立關系?我還發現了一些關于多態關系的東西,但我不確定它是否適用于此
查看完整描述

2 回答

?
慕尼黑8549860

TA貢獻1818條經驗 獲得超11個贊

如果你的表中沒有更復雜的依賴關系,我建議你使用多態關系。創建一個表:


public function up()

{

    Schema::create('processes', function (BlueprintCustom $table) {

        $table->morphs('process');

        $this->timestamp('processing_started_at')->nullable();

        $this->timestamp('processing_ended_at')->nullable();

    });

}

并將您的關系添加到您的相關模型。這將緩解從同一領域到您的遷移。


查看完整回答
反對 回復 2023-04-15
?
小唯快跑啊

TA貢獻1863條經驗 獲得超2個贊

您可以創建一個繼承自的自定義藍圖類


Illuminate\Database\Schema\Blueprint

是這樣的:


namespace App\Whatever;


use Illuminate\Database\Schema\Blueprint;


class BlueprintCustom extends Blueprint {

    public function customfields()

    {

        $this->string('source')->nullable();

        $this->timestamp('processing_started_at')->nullable();

        $this->timestamp('processing_ended_at')->nullable();

    }

}

那么,在遷移時,您可以執行以下操作:


use App\Whatever\BlueprintCustom;


public function up()

{

    Schema::create('newtable', function (BlueprintCustom $table) {

        $table->increments('id');

        $table->string('blah');

        $table->customfields();  //This adds your fields

        $table->timestamps();

    });

}


查看完整回答
反對 回復 2023-04-15
  • 2 回答
  • 0 關注
  • 155 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號