How to get a variable that is in the controller and put inside a view (laravel)

2

I'm playing with laravel and then came the following doubt. I need to get a variable that is in the controller and put it inside a view. In case I am sending an email with data contained in a form.

Basically this is how the user types the information in the form field when clicking on send the controller receives the variables from within the form validates and sends the email. The problem is that the email view is empty. I need to get these variables from the controller and put it in another view, this being the email view. VIEW THE FORM.

<section class="form-content content-bottom wow fadeIn" data-wow-duration="0.9s">
    <h4>Envie-nos uma mensagem</h4> <br>
    <div class="container">

        <div class="row wow fadeIn" data-wow-duration="1.0s">
           <form class="col s12" name ="form1" role="form" method="POST" action="{{ action('ContatoController@enviar') }}" >
                {{ csrf_field() }}
                <div class="row">
                    <div class="input-field col s12 m6 l6">
                        <input id="nome" type="text" class="validate" name="nome" required value="" placeholder="Nome Completo">
                        <label for="nome">Nome</label>
                    </div>
                    <div class="input-field col s12 m6 l6 ">
                        <input id="email" type="email" class="validate" name="email" required value="">
                        <label for="email">E-mail</label>
                    </div>
                </div>
                <div class="row">
                    <div class="input-field col s12">
                        <input id="phone" type="text" pattern="[0-9]+$" name="phone" required value="" minlength="10" maxlength="15" placeholder="telefone (DD)xxxx-xxxx ou celular (DD)9xxxx-xxxx *somente numeros">
                        <label for="phone">Telefone</label>
                    </div>
                </div>

                <div class="row">
                    <div class="input-field col s12">
                        <input id="assunto" type="text" name="assunto" required value="">
                        <label for="assunto">Assunto</label>
                    </div>
                </div>
                <div class="row">
                    <div class="input-field col s12">
                        <textarea id="mensagem" name="mensagem" class="materialize-textarea" required value=""></textarea>
                        <label for="mensagem">Mensagem</label>
                    </div>
                </div>

                <div class="row center">
                    <div class="col s12">
                        <button type="submit" value="submit" class="btn login-button" onclick="return validar()">
                            Enviar
                        </button>
                      <br><br>
                    </div>
                </div>
            </form>
        </div>
    </div>
</section>

CONTROLLER

    <?php

namespace App\Http\Controllers;

use App\Mail\msgformulario;
use App\Mail\msgfeedback;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\Http\Controllers\Controller;



class ContatoController extends Controller
{
    public function enviar(request $request) {
        $nome = $request->input('nome');
        $email = $request->input('email');
        $phone = $request->input('phone');
        $assunto = $request ->input('assunto');
        $mensagem = $request->input('mensagem');
        if(isset($nome) && empty($nome)==false){
            if(isset($email) && empty($email)==false){
                if (isset($phone) && empty($phone)==false){
                    if (isset($assunto) && empty($assunto)==false){
                        if (isset($mensagem) && empty($mensagem)==false){
                            mail::to('[email protected]')->send(new msgformulario);
                            mail::to($email)->send(new msgfeedback);
                            $this->load->view('emails.formulario',$assunto);
                            return redirect('/contato');
                        }
                    }
                }
            }
        }else{
            return redirect('/contato');
            }               

    }
}
?>

ROUTE:

Route::post('/feedback','ContatoController@enviar');

EMAIL VIEW (which should receive the variables to be filled in.)

    <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title> Bem Vindo </title>
</head>
<body>

</body>
</html>
    
asked by anonymous 17.02.2017 / 19:45

3 answers

1

There is a request that Laravel calls CSRF, a token to ensure data security.

In your view, this is missing as the first input of your form:

    <form>
            <input type="hidden" name="_token" value="{{{csrf_token()}}}"    />
    </form>
    
19.08.2017 / 21:57
0

In your View, just put it like this:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title> Bem Vindo </title>
</head>
<body>
    {{$nome}}
    {{$email}}
    {{$phone}}
    {{$assunto}}
    {{$mensagem}}
</body>
</html>

No controller would be:

<?php namespace App\Http\Controllers;

use App\Mail\msgformulario;
use App\Mail\msgfeedback;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\Http\Controllers\Controller;

class ContatoController extends Controller
{
    public function enviar(request $request) 
    {
        $nome = $request->input('nome');
        $email = $request->input('email');
        $phone = $request->input('phone');
        $assunto = $request ->input('assunto');
        $mensagem = $request->input('mensagem');
        if(isset($nome) && empty($nome)==false) &&
           isset($email) && empty($email)==false &&
           isset($phone) && empty($phone)==false &&
           isset($phone) && empty($phone)==false &&
           isset($assunto) && empty($assunto)==false &&
           isset($mensagem) && empty($mensagem)==false)
            {
                $data = $request->all();
                Mail::send('pagina.da.view', $data, function($message) use ($data)
                {                   
                    $message->from($data['email'], $data['nome']);
                    $message->to($data['email']);                           
                    $message->subject('Envio de Email');                
                });                
            }
        }
        else
        {    
            return redirect('/contato');
        }               
    }
}
    
17.02.2017 / 20:32
0

As the colleague said shortly above the CSRF

 <form method="POST" action="{{ route('suarota') }}">
        @csrf //se estiver usando o blade só adicionar isso no form
    </form>
    
27.07.2018 / 21:12