[英]Laravel create simple drop-down from relationship and concat columns
[英]How to add drop-down list & toggle switch columns into migration table columns in PHP Laravel?
我使用 PHP 和 Laravel 框架。
在我的前端頁面中,我的表格包含鏈接、評論、日期、數字、切換開關等信息
現在對於我的后端,我將列添加到遷移文件夾下的表中。 我知道如何為我的鏈接添加類型( $table->string('my_link');
),評論( $table->text('my_comments');
),日期( $table->dateTime('my_date');
) 數字 ( $table->decimal('my_number',20,3);
) 等
但是我不確定我應該選擇https://laravel.com/docs/9.x/migrations下的哪種類型來切換開關和下拉列表數據。
如果我使用Boolean來保存撥動開關數據是否合理? 例如$table->boolean('confirmed');
如果我使用$table->integer('my_drop_down_list')->unsigned();
是否合理? $table->foreign('my_drop_down_list')->references('id')->on('users')->onUpdate('NO ACTION')->onDelete('NO ACTION');
保存下拉列表數據?
假設您的表單中有一個這樣的切換按鈕。
<input class="toggle-class" type="checkbox" name="confirmed">
在您的 controller 中,您將檢查切換按鈕是on
還是Off
。
$confirmed = $request->confirmed? 1:0; // 1 if toggle on 0 if off
$model->confirmed = $confirmed; // replace $model with name of your model
$model->save() or $model->update();
注意:根據您的代碼更新變量。
我認為你應該在你的表中這樣做:
country_id
字段很大 integer - 表中的外鍵。 confirmed
字段是表中的枚舉數據類型。
Schema::create('states', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('country_id');
$table->timestamps();
$table->enum('confirmed',['0','1'])->default('0');
$table->foreign('country_id')->references('id')->on('countries')->onDelete('cascade');
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.