LoginSignup
1
1

More than 1 year has passed since last update.

[laravel]formから複数レコードを一括で登録する

Last updated at Posted at 2023-02-20

はじめに

formを使ってユーザーが入力した値を1レコードDBへ登録する処理はチュートリアルだったりでもよくあるが、
複数のレコードを一括で登録する方法があまりネットに転がってなかったので備忘録として残す
あまりよくあるパターンではないのかもしれないが、誰かの役に立てば嬉しい

今回実装したいこと

上記にも書いてる通り、ユーザーがformに入力した複数のレコードの値をsubmitボタン1つで一括登録したい
あまりケースとして多くはないのかもしれないが具体的には以下の感じでやっていく

スクリーンショット 2023-02-20 17.07.53.png

画像な感じで4つのレコードが入力できるようになっており、
それを1つのsubmitからDBへ登録する流れ。
実際にコードで書いていく。

View

TrRecordSupport.blade.php
            <form method="post" action="{{ url('/trsupportrecords') }}">
                {{ csrf_field() }} <!-- CSRF対策 -->
                @for ($i=0; $i<4; $i++)
                    <tr>
                        <td><input type="date" name="date[]" class="form-control" value="<?php echo date($today);?>" placeholder="日付"></td>
                        <td><input type="text" name="menu[]" class="form-control" value="{{ old('menu')}}" placeholder="トレーニングメニュー"></td>
                        <td><input type="number" name="weight_first[]" class="form-control" value="{{ old('weight_first')}}" placeholder="1セット目重量(KG)"></td>
                        <td><input type="number" name="reps_first[]" class="form-control" value="{{ old('reps_first')}}" placeholder="1セット目回数(回)"></td>
                        <td><input type="number" name="weight_second[]" class="form-control" value="{{ old('weight_second')}}" placeholder="2セット目重量(KG)"></td>
                        <td><input type="number" name="reps_second[]" class="form-control" value="{{ old('reps_second')}}" placeholder="2セット目回数(回)"></td>
                        <td><input type="number" name="weight_third[]" class="form-control" value="{{ old('weight_third')}}" placeholder="3セット目重量(KG)"></td>
                        <td><input type="number" name="reps_third[]" class="form-control" value="{{ old('reps_third')}}" placeholder="3セット目回数(回)"></td>
                    <tr>
                    <input type="hidden" name="num[]">
                @endfor
                <td><input class="btn btn-info btn-sm" type="submit" value="送信"></td>
            </form>
  • 今回4つのレコードと決まっているのでfor文を用い、4つ登録するようにした

Controller

TrRecordSupportController.php
    public function create(Request $request) {

        $group = $request->group;
        $trSupportMenus = TrSupportMenu::where('group', $group)->get();
        $today = date("Y-m-d");
        return view('trsupportrecords.create', compact('trSupportMenus', 'today'));
    }

    public function store(Request $request) {

        $i = 0;
        foreach($request->num as $val) {
            $trSupportRecord = new TrSupportRecord();
            $trSupportRecord->user_id = Auth::id();
            $trSupportRecord->date = $request->date[$i];
            $trSupportRecord->menu = $request->menu[$i];
            $trSupportRecord->weight_first = $request->weight_first[$i];
            $trSupportRecord->reps_first = $request->reps_first[$i];
            $trSupportRecord->weight_second = $request->weight_second[$i];
            $trSupportRecord->reps_second = $request->reps_second[$i];
            $trSupportRecord->weight_third = $request->weight_third[$i];
            $trSupportRecord->reps_third = $request->reps_third[$i];
            $trSupportRecord->save();
            $i++;
        }

        return redirect()->route('trsupportrecords.index')->with(
            'message', '登録が完了しました'
        );

    }
  • foreach文の中でインスタンス化をしてあげる

これでDBを確認すると4つのレコードが1つの登録ボタンからされる。

他に良いやり方がありましたらご教示願います。
以上。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1