##現状
ControllerでModel(Client.php)から引っ張ってきたデータを、checkClient.blade.phpというビュー上にforeachで展開するというコードを記述しました。
しかし、foreachがselect boxを生み出す<select>タグにまたがっており、Client.phpのカラムの数だけselect boxが生み出されるという形になっています。
foreachがselect boxを生み出す<select>タグにまたがらないようにすればいいということはわかるのですが、
php
1@foreach($list as $val) 2 <form method="POST" action="/toAddInvoice/{{$val->id}}" id="clients_form"> 3 {{csrf_field()}} 4 <div class="input-field col s12"> 5 <select id="client_id" class="select" name="client_id" onchange="submit(this.form)"> 6 <option value="" disabled selected>クライアントを選んでください。</option> 7 <option value="{{$val->id}}">{{$val->client_name}}</option> 8 </select> 9 <label for="client_id">クライアント</label> 10 </div> 11 </form> 12 @endforeach
というコードを書いており、formのactionの中に入っている{{$val->id}}の数値と、<option>のvalueである{{$val->id}}を同一のものにしたいと考えています。
しかし、@foreach〜@endforeachを2つ作り、<form>タグと<option>タグそれぞれを囲うとなると、formのactionの中に入っている{{$val->id}}の数値と、<option>のvalueである{{$val->id}}を同一のものになりません。(具体的には、formのactionの中に入っている{{$val->id}}の数値が1になってしまいます。)
##叶えたいこと
・select boxを1つだけにし、カラムの分だけのoptionができるようにしたい。
・formのactionの中に入っている{{$val->id}}の数値と、<option>のvalueである{{$val->id}}を同一のものにしたい
どのような修正が必要か教えていただきたいです。
よろしくお願いいたします。
##問題のコード
●checkClient.blade.php(一部抜粋)
php
1@foreach($list as $val) 2 <form method="POST" action="/toAddInvoice/{{$val->id}}" id="clients_form"> 3 {{csrf_field()}} 4 <div class="input-field col s12"> 5 <select id="client_id" class="select" name="client_id" onchange="submit(this.form)"> 6 <option value="" disabled selected>クライアントを選んでください。</option> 7 <option value="{{$val->id}}">{{$val->client_name}}</option> 8 </select> 9 <label for="client_id">クライアント</label> 10 </div> 11 </form> 12 @endforeach
●InvoiceController.php(一部抜粋)
php
1public function checkClient(){ 2 $list = Client::all(); 3 return view('invoice.checkClient', compact('list')); 4 }
●Client.php(一部抜粋)
php
1public function up() 2 { 3 Schema::create('clients', function (Blueprint $table) { 4 $table->increments('id'); 5 $table->integer('user_id')->unsigned(); 6 $table->string('client_name'); //クライアント名 7 $table->string('personnel'); //担当者 8 $table->string('client_tel_number'); //TEL 9 $table->text('client_address'); //住所 10 $table->decimal('sales_tax_rate'); //消費税率 11 $table->decimal('withholding_tax_rate'); //源泉徴収税率 12 $table->integer('tax_category'); //税区分 13 $table->integer('fraction'); //端数処理 14 $table->timestamps(); 15 $table 16 ->foreign('user_id') 17 ->references('id') 18 ->on('users') 19 ->onDelete('cascade'); 20 });
##追記
最終的に以下のようなHTMLができることが理想です。
php
1<form method="POST" action="/toAddInvoice/{{$val->id}}" id="clients_form"> 2 <div class="input-field col s12"> 3 <select id="client_id" class="select" name="client_id" onchange="submit(this.form)"> 4 <option value="" disabled selected>クライアントを選んでください。</option> 5 <option value="1">クライアント1</option> 6 <option value="2">クライアント2</option> 7 <option value="3">クライアント3</option> 8 </select> 9 <label for="client_id">クライアント</label> 10 </div> 11</form>