Home > Net >  How to add drop-down list & toggle switch columns into migration table columns in PHP Laravel?
How to add drop-down list & toggle switch columns into migration table columns in PHP Laravel?

Time:03-29

I am use PHP with Laravel framework.

In my front-end page, my table contains information for link, comment, date, number, toggle switch etc

Now for my back-end, I am adding columns into the table under migration folder. I know how to add the type for my link($table->string('my_link');), comment($table->text('my_comments');), date($table->dateTime('my_date');)number ( $table->decimal('my_number',20,3);) etc

But I am not sure which type under https://laravel.com/docs/9.x/migrations should I choice for toggle switch and drop-dwon list data.

Is it reasonable if I use Boolean to save toggle switch data? For example $table->boolean('confirmed');

And is it reasonable if I use $table->integer('my_drop_down_list')->unsigned(); $table->foreign('my_drop_down_list')->references('id')->on('users')->onUpdate('NO ACTION')->onDelete('NO ACTION');to save drop-down list data?

CodePudding user response:

Suppose you have a toggle button in your form like this.

<input   type="checkbox" name="confirmed">

In your controller you will check if toggle button is on or 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();

Note : Update variables according to your code.

CodePudding user response:

I think you should do this into your table:

country_id field is big integer - foreign key in the table. confirmed field is enum datatype in the table.

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');
});
  • Related