Laravel9で問い合わせフォームを作成しました。
問い合わせフォーム内に以下のradioボタンがあります。
contact.blade.php
1<td class="form"> 2 <div class="form-radio"> 3 <input class="input" type="radio" name="radio" 4 id="contact" value="お問合せ" {{ old('radio') === 'お問合せ' ? 'checked' : '' }}/> 5 <label class="label" for="contact">お問合せ</label> 6 </div> 7 <div class="form-radio"> 8 <input class="input" type="radio" name="radio" 9 id="quote" value="お見積依頼" {{ old('radio') === 'お見積依頼' ? 'checked' : '' }}/> 10 <label class="label" for="quote">お見積依頼</label> 11 </div> 12 <div class="form-radio-"> 13 <input class="input" type="radio" name="radio" 14 id="other" value="その他" {{ old('radio') === 'その他' ? 'checked' : '' }}/> 15 <label class="label" for="other">その他</label> 16 </div> 17</td>
問い合わせフォームを入力 -> 入力確認画面(confirm.blade.php
)では以下のコードでtype="radio"のvalueが取得できています。
confirm.blade.php
1<p> {{$inputs['radio']}}</p>
確認画面 -> Mailableクラスを使用して問い合わせを送信するのに、app/Mail/ContactsSendmail.php を作成。以下のように記述しました。
ContactsSendmail.php
1<?php 2 3namespace App\Mail; 4 5use Illuminate\Bus\Queueable; 6use Illuminate\Contracts\Queue\ShouldQueue; 7use Illuminate\Mail\Mailable; 8use Illuminate\Queue\SerializesModels; 9 10class ContactsSendmail extends Mailable 11{ 12 use Queueable, SerializesModels; 13 14 // プロパティを定義 15 private $radio; 16 private $company_name; 17 private $contact_name; 18 private $kana; 19 private $tell; 20 private $zip; 21 private $add; 22 private $message; 23 24 /** 25 * Create a new message instance. 26 * 27 * @return void 28 */ 29 public function __construct($inputs) 30 { 31 // コンストラクタでプロパティに値を格納 32 $this->radio = $inputs['radio']; 33 $this->company_name = $inputs['company_name']; 34 $this->contact_name = $inputs['contact_name']; 35 $this->kana = $inputs['kana']; 36 $this->tell = $inputs['tell']; 37 $this->zip = $inputs['zip']; 38 $this->add = $inputs['add']; 39 $this->email = $inputs['email']; 40 $this->body = $inputs['body']; 41 } 42 43 /** 44 * Build the message. 45 * 46 * @return $this 47 */ 48 public function build() 49 { 50 // メールの設定 51 return $this 52 ->from('hello@example.com') 53 ->subject('自動送信メール') 54 ->view('contact.mail') 55 ->with([ 56 'radio' => $this->radio, 57 'company_name' => $this->company_name, 58 'contact_name' => $this->contact_name, 59 'kana' => $this->kana, 60 'tell' => $this->tell, 61 'zip' => $this->zip, 62 'add' => $this->add, 63 'email' => $this->email, 64 'body' => $this->body, 65 ]); 66 } 67} 68
しかし、以下の箇所で「Undefined array key "radio"」のエラーが出ています。
ContactsSendmail.php
1 // コンストラクタでプロパティに値を格納 2 $this->radio = $inputs['radio'];
配列から存在しないkeyを指定していると「Undefined array key "XXX"」というエラーになるようなのですが、入力確認画面では以下のコードでtype="radio"のvalueが取得、表示されているのに、Mailableクラスを使用して問い合わせを送信する際にはなぜエラーになってしまうのでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/10/26 00:52