新規登録をする際、Viewに3つのinput textタグがあり、登録ボタンを押すと、
データベース上に3つのレコードが登録されるという機能の実装を考えております。
この3つの入力枠は全て入力必須です。また、表示順といった概念を保持する必要があります。
そこで次のような記述をしてあります。
View
{!! Form::open(['url' => route("/")]) !!} {{ csrf_field() }} {!! (Form::text('text') !!} {!! (Form::text('text') !!} {!! (Form::text('text') !!} {!! Form::close() !!}
Controller
public function create(Request $request) { $test = new \App\Test(); $test -> position = 1; // ここが一度の登録で1,2,3で登録できるようにしたいです $test -> text = $request -> text; // Viewの input text name="text" の値をDBのtextカラムに登録します。 $test -> save(); }
DBに登録されるレコードのイメージとしては次のようなものです。
テーブル "public.tests" 列 | 型 | 修飾語 ------------+--------------------------------+----------------------------------------------------------- id | integer | not null default nextval('tests_id_seq'::regclass) text | text | not null position | integer | not null default 0 created_at | timestamp(0) without time zone | updated_at | timestamp(0) without time zone | id | text | position | created_at | updated_at ----+------+----------+---------------------+--------------------- 1 | A | 1 | 2016-08-09 13:00:00 | 2016-08-09 13:00:00 2 | B | 2 | 2016-08-09 13:00:00 | 2016-08-09 13:00:00 3 | C | 3 | 2016-08-09 13:00:00 | 2016-08-09 13:00:00 4 | D | 1 | 2016-08-09 14:00:00 | 2016-08-09 14:00:00 5 | E | 2 | 2016-08-09 14:00:00 | 2016-08-09 14:00:00 6 | F | 3 | 2016-08-09 14:00:00 | 2016-08-09 14:00:00
Viewに表示順を入力する欄はなく、input text の一番上が position = 1 次が 2 最後が 3 といった風に登録されるようにしたいです。
hiddenなどで値を送信すると良いでしょうか?
実際のテーブルではこの複数登録する部分はリレーションテーブルになっており、
もっと複雑ですが一旦は複数行の登録を可能にする方法が知りたいです。
何か良いアイデアはございますでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/08/09 10:09 編集