How to take a variable that is in the controller and put inside a view (Adjustable)

Asked

Viewed 2,508 times

2

I’m messing with Lockable and from there came the following doubt. I need to take a variable that is in the controller and put inside a view. In case I am sending an email with data contained in a form.

Basically, the user enters the information in the form field by clicking send the controller receives the variables from within the form and sends the email. The problem is that the email view is empty. I need to take these controller variables and put them in another view being the email view. VIEW OF 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');

VIEW OF THE EMAIL(that must receive the variables to be completed.)

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

</body>
</html>
  • just create an HTML page using blade ({{}} ou {!!!!}) and the normal formatting, that the structure to send e-mail via html is as example on the site, this is your doubt? you have developed some code if yes can add in your question ?

  • I put the codes in the question

  • What version of Laravel?

  • I wore the compact in the Laravel

  • in the controller, in the function that renders the view you create the variable receiving the desired values, type: $myvar = "myvalue" then in the function that renders the view you pass the Compact as parameter and put the variable like this: Compact('myvar')

  • https://laravel.com/docs/5.4/views#Creating-views

Show 1 more comment

3 answers

1

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

In your view this is missing as your form’s first input:

    <form>
            <input type="hidden" name="_token" value="{{{csrf_token()}}}"    />
    </form>

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>

In the 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');
        }               
    }
}
  • 1

    the version of the Laravel is the 5.3, ok thank you very much, I will test here and return with what happened.

  • Gave error :/ Tokenmismatchexception in Compiled.php line 3314:

  • The error is the token some problem in the post request has nothing to do with the code posted, the error is not in it, at least until now. @Leonardo

0

As the colleague said just above missing the CSRF

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.