駆け出しエンジニアの勉強記録

アラフォー女の未経験すぎる業界での勉強記録

HTML→Formファサードに変換


Formファサードを以下のコマンド入力でインストール必要。
composer require laravelcollective/html

基本
{{ Form :: type属性 ( 'name属性' , 'value属性' , [ 'オプション' ]) }}

【上記フォーム作成】

①送信に必要
<form method="POST" action="{{ route('form-post') }}">

↓変換

 {{Form::open()}}  {{Form::close()}}
で以下のコードを挟む

②名前って書かれてるラベル
<label for="namelab" class="block text-gray-700 text-sm font-bold mb-2">名前</label>

↓変換

{{Form::label('namelab','名前',['class' => "block text-gray-700 text-sm font-bold mb-2"])}}

③名前入力枠
<input type="text" name="name" id="namelab" class="border border-black" value="{{ old('name') }}" />
< p style = "color : red" > {{ $errors -> first ( 'name' ) }} </p>
↓変換

{{Form::text('name',null,['id'=>'namelab','class'=>"border border-black"])}}
{!! $errors->first('name','<p style="color: red">:message</p>') !!}
※リクエストファイルに書いたバリデーション設定があるので引っかかった時はエラーメッセージが表示され、(inputではvalueで表示していた)入力した文字も消えずにそのまま残る

④メールアドレス、パスワードも同様

⑤新規登録ボタン
<button class="bg-blue-500 hover:bg-blue-600 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline-blue active:bg-blue-700">
新規登録
</button>

【その他】

ラジオボタン
<label> < input type = "radio" name = "gender" value = "男性" {{ old( 'gender' , ( $form_data[ 'gender' ] ?? '' )) === '男性' ? "checked" : '' }}>男 </label>
<label> < input type = "radio" name = "gender" value = "女性" {{ old('gender' , ( $form_data[ 'gender' ] ?? '' )) === '女性' ? 'checked' : '' }}>女 </label>
<label> < input type = "radio" name = "gender" value = "その他" {{ old( 'gender' , ( $form_data[ 'gender' ] ?? '' )) === 'その他' ? 'checked' : '' }}>その他 </label>
<p style="color:red">{{ $errors->first('gender') }}</p>

↓変換

<label> {{ Form :: radio( 'gender', '男性', true ) }} 男 </label>
<label> {{ Form :: radio( 'gender', '女性' ) }} 女 </label>
<label> {{ Form :: radio( 'gender', 'その他' ) }} その他 </label>
{ !! $errors->first('gender', '<p style="color:red">:message</p>') !! }

プルダウン
※config/params.phpに書いた配列を回す
<select name = "pref">            
 < option value = "selected" >お住まいの地域< /option > //最初に表示されてるもの
 @foreach ( config ( 'params.prefectures' ) as $prefecture )
  <option value = "{{ $prefecture}}" > {{$prefecture}} </option> //  valueがリクエストで送信される値 
 @endforeach            
</select>    

↓変換

{{ Form :: select( 'pref' , [ '' => 'お住まいの地域' ] + config( 'params.prefectures' )) }}

※paramsの配列を使わない場合
<select class = "form-control" id = "evalute" name = "selectEvaluate"> 
 <option value = "good">よかった</option>
 <option value = "ordinarily" selected = "selected">普通</option>
 <option value = "bad">悪い</option>
</select>

hidden
<input type = "hidden" name = "id" value = "{{ $blogdata -> id }}">

↓変換

{{ Form :: hidden( 'id'$blogdata -> id ) }}