I'm trying to send an email from a contact form. But it returns the error "Trying to get property 'email' of non-object."
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Mail\ContatoEmail;
use Illuminate\Support\Facades\Mail;
use App\Http\Requests\ContactFormRequest;
class MailController extends Controller {
public function create()
{
return view('home');
}
public function store(Request $request)
{
$contact = [];
$contact['nome'] = $request->get('nome');
$contact['email'] = $request->get('email');
$contact['msg'] = $request->get('msg');
Mail::to(config('mail.support.address'))->send(new ContatoEmail($contact));
flash('Sua mensagem foi enviada com sucesso!')->success();
return redirect()->route('home');
}
}
View
{!! Form::open(['route' => 'home.store', 'class' => 'form']) !!}
<div class="form-group">
{!! Form::label('nome', 'Nome') !!}
{!! Form::text('nome', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('email', 'E-mail') !!}
{!! Form::text('email', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('msg', 'Descreva seu projeto') !!}
{!! Form::textarea('msg', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('Enviar', ['class' => 'btn btn-primary btn-lg btn-block']) !!}
</div>
{!! Form::close() !!}
App \ Mail
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class ContatoEmail extends Mailable
{
use Queueable, SerializesModels;
public $contact;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($contact)
{
$this->contact = $contact;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this
->to(config('mail.support.address'))
->subject('Pedido orçamento site')
->view('emails.contact');
}
}
Route
Route::get('home', 'MailController@create')->name('contact');
Route::post('home', 'MailController@store')->name('home.store');