💭
【音声ファイルアップロード機能】laravel migration カラムを追加
※すでにあるテーブル(migrationファイル)にカラムを追加してphp artisan migrateしても良いが、そうするとロールバックする時に down()で テーブル自体がなくなってしまう
① migrationファイルを作る
既存のテーブルに変更を加える場合には、--create オプションではなく、--table オプションを使って、テーブル名を指定
php artisan make:migration add_user_name_to_music_files_table --table=music_files
② migrationファイルを編集
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddUserNameToMusicFilesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('music_files', function (Blueprint $table) {
$table->string('user_name'); //カラム追加
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('music_files', function (Blueprint $table) {
$table->dropColumn('user_name'); //カラムの削除
});
}
}
③ migrationする
php artisan migrate
★ 確認
ファイルアップデートするには追加でapi側はモデル、コントローラ、フロント側はフォームにカラムを追記する
順番が気持ち悪い場合はafter()を使用する
参考
Discussion