代码之家  ›  专栏  ›  技术社区  ›  Gregory Rothstein

Laravel 11表单数据未传递到电子邮件模板

  •  0
  • Gregory Rothstein  · 技术社区  · 9 月前

    我对Laravel的使用相当陌生。但问题是,我遇到了一个问题,即点击提交后从表单中获取的数据没有传递到刀片模板。例如,电子邮件发送到电子邮件地址,但在电子邮件中,表单中的名称不会显示在电子邮件中。我应该说我正在使用Laravel和Livewire。

    first_name的输入之一:

     <x-wui-input wire:model="first_name" placeholder="First Name" label="First Name *[Required]" type="text" id="firstName" name="first_name" class="block w-full px-4 py-4 mx-1 mt-1 border-gray-300 rounded-md focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm" />
    

    电子邮件刀片模板:

    <div class="p-4 bg-white rounded-lg shadow-md">
        <h2 class="mb-4 text-2xl font-bold text-gray-800">Thank You for Submitting a Good Deed!</h2>
        <p class="mb-2 text-gray-600">Thank you {{$data['first_name']}} {{$data['last_name']}}  for nomination for the annual Chasing Good Award.</p>
    </div>
    

    表单组件(Submit方法):

    public function submit(Request $request)
        {
            $this->validate();
    
            $data = [
                'first_name' => $request->first_name,
                'last_name' => $request->last_name,
                'nominee_name' => $request->nominee_name
            ];
    
            Nomination::create(
                $this->only([
                    'first_name',
                    'last_name',
                    'email_address',
                    'phone_number',
                    'nominating_category',
                    'nominee_name',
                    'nominee_email',
                    'nj_county',
                    'story_essay',
                    'uploaded_video',
                    'disclaimer_agreed'
                ])
            );
    
            // Create a new Mailable instance
            $mailable = new ThankYouMailable($data);
            $mailabe2 = new NominationMailable($data);
            $mailable3 = new ThankYouSelfMailable($data);
    
            if ($this->email_address == $this->nominee_email) {
                Mail::to($this->email_address)->send($mailable3);
            } else {
                Mail::to($this->email_address)->send($mailable);
                Mail::to($this->nominee_email)->send($mailabe2);
            }
    
            session()->flash('message', 'Good deed submitted successfully!');
    
            return back()->with('message', 'Good deed submitted successfully!');
        }
    

    可邮寄类:

    class ThankYouSelfMailable extends Mailable
    {
        use Queueable, SerializesModels;
    
        public $data;
    
        /**
         * Create a new message instance.
         */
        public function __construct($data)
        {
            $this->data = $data;
        }
    
        public function build()
        {
            return $this->view('emails.thank_you_self_nominator')->with($this->data, $this->data);
        }
    
        /**
         * Get the message envelope.
         */
        public function envelope(): Envelope
        {
            return new Envelope(
                subject: 'Thank You For Submitting',
            );
        }
    
        /**
         * Get the message content definition.
         */
        public function content(): Content
        {
            return new Content(
                view: 'emails.thank_you_self_nominator',
            );
        }
    
        /**
         * Get the attachments for the message.
         *
         * @return array<int, \Illuminate\Mail\Mailables\Attachment>
         */
        public function attachments(): array
        {
            return [];
        }
    }
    
    1 回复  |  直到 9 月前
        1
  •  0
  •   Gert B.    9 月前

    在livewire组件中,您不使用request对象,而应该使用 $this :

    $data = [
        'first_name' => $this->first_name,
        'last_name' => $this->last_name,
        'nominee_name' => $this->nominee_name
    ];