LoginSignup
0
0

More than 1 year has passed since last update.

PHP、Laravelのbladeファイルの中でプルダウン形式を使用した

Last updated at Posted at 2022-04-12

select文の中にif文を記述して、
プルダウン形式を記述したときの内容をまとめます。

行いたい処理。
3種類の会員(無料会員 id=1、トライアル会員 id =2、プラミアム会員 id=4)
がコントーローラーから配列渡せれてきて、
プルダウン方式で選択し更新できるようにしたい。

結果のコード。

show.blade.php
<form id="edit" action="{{ route('memberlist.update', ['id'=>$user['id']]) }}" method="post" class="contents-memberdetails-remarks">

<select name="user_type">
<option></option>
@if ($user['user_type'] == 1)
<option value="1" name="user_type" selected>無料会員</option>
<option value="2" name="user_type" >トライアル会員</option>
<option value="4" name="user_type" >プレミアム会員</option>
@elseif($user['user_type'] == 2)
<option value="1" name="user_type" selected>無料会員</option>
<option value="2" name="user_type" >トライアル会員</option>
<option value="4" name="user_type" >プレミアム会員</option>
@else
<option value="1" name="user_type" selected>無料会員</option>
<option value="2" name="user_type" >トライアル会員</option>
<option value="4" name="user_type" >プレミアム会員</option>
@endif
</select>

記述内容説明。
@if文で
ユーザータイプ1なら無料会員。
ユーザータイプ2ならトライアル会員。
ユーザータイプ4ならプレミアム会員。
を表示させる記述。

selectedの記述は、デフォルトで表示させたい項目を指定できるので、
ユーザータイプ1の項目は無料会員の欄に記述。
ユーザータイプ2の項目はトライアル会員の欄に記述。
ユーザータイプ4の項目はプレミアム会員の欄に記述。
しました。

0
0
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
0
0