性別を選択してもらった後、確認画面で文字で表示させ データベースへ数値として送信したい
解決したいこと
ラジオボタンで性別を選択してもらった後、確認画面で男性、または女性を文字で表示させ
データベースへ数値として送信したいです。
発生している問題・エラー
inputタグの後にlabelタグを追加して色々試しましたが
Undefined indexや
SQLSTATE[HY000]: General error: 1364 Field 'gender' doesn't have a default value (SQL: insert into `contacts(...)
等のエラーがでてしまっています。
該当するソースコード
フォーム画面
<div class="contact-form__content">
<div class="contact-form__heading">
<h2>お問い合わせ</h2>
</div>
<form class="form" action="contacts/confirm" method="post">
<div class="form__group">
<div class="form__group-title">
<span class="form__label--item">性別</span>
<span class="form__label--required">※</span>
</div>
<div class="form__group-content">
<div class="form__input--radio">
<input id="men" type="radio" name="gender" value="1" checked>
<label for="men" name="gender2" value="男性">男性</label>
<input id="women" type="radio" name="gender" value="2">
<label for="women" name="gender2" value="女性">女性</label>
</div>
</div>
</div>
</form>
</div>
確認画面
<tr class="confirm-table__row">
<th class="confirm-table__header">性別</th>
<td class="confirm-table__text">
<input type="text" name="gender" value="{{ $contact['gender2'] }}" readonly />
</td>
</tr>
マイグレーションファイル
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateContactsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('contacts', function (Blueprint $table) {
$table->string('fullname');
$table->tinyInteger('gender')->comment('性別:1=男性、2=女性');
$table->string('email');
$table->char('postcode');
$table->string('address');
$table->string('building_name')->nullable();
$table->text('opinion', 120);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('contacts');
}
}
コントローラー
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Contact;
class ContactController extends Controller
{
public function index()
{
return view('index');
}
public function confirm(Request $request)
{
$contact = $request->only(['last_name', 'first_name', 'gender2', 'email', 'postcode', 'address', 'building_name', 'opinion']);
return view('confirm', ['contact' => $contact]);
}
public function store(Request $request)
{
$contact = (['fullname' => $request->last_name . $request->first_name] + $request->only(['gender', 'email', 'postcode', 'address', 'building_name', 'opinion']));
Contact::create($contact);
return view('thanks');
}
}
自分で試したこと・考えていること
上記のソースコードでは一部省略しております。足りなり部分があれば指摘お願い致します。
このコードでは確認画面の段階でUndefined indexエラーがでます。
まず自分がやろうとしていることは、valueで初期値を設定して確認画面で表示させるために
controllerでcontact変数を定義してconfirmで呼び出す。
(この時に文字で表示させたいのでlabelタグに別のnameと初期値を定義してはどうかと思ったのでこういった記述になりました。)
その後、store関数の方でデータべースへtinuint型でデータを送りたいのでinputタグのnameを使用して$contactに入れています。
問題はinputタグやlavelタグの属性の使い方かと思いますが、浅学のため解決法を調べてもわかりませんでした。
ご教授お願い致します。
【追記・解決方法】
アドバイスのおかげで解決できました。
<label>タグからgender2を消して、controllerのgender2をgenderに変更。
そして、確認ページで条件分岐させればできました。
formタグでデータ数字で送信するのでinputタグのtype属性はhiddenに変更しました。
確認画面
<input type="hidden" name="gender" value="{{ $contact['gender'] }}" readonly />
<?php
if ($contact['gender'] == '1') {
echo '男性';
} else if ($contact['gender'] == '2') {
echo '女性';
}
?>