In laravel how to send and invoke more data with Markdowns Mail

0

I have this question of how to send several data in a array and that when invoking this data in the template that was sent can declare them in different sections, this is what I have

This is my Mailable

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class EncuestaSolvex extends Mailable
{
    use Queueable, SerializesModels;

    public $data;
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this
        ->from('[email protected]')
        ->subject('Resultados de Encuesta')
        ->markdown('emails.encuesta')
        ->with(['data', $this->data]);
    }
}

This is the driver part

public function email(Request $request)
    {
        $email=DB::table('usuario')
        ->where('id_usu', '=', $request->get('id'))
        ->get(); 
        $nombre=DB::table('usuario')
        ->where('id_usu', '=', $request->get('id'))
        ->get(); 
        $usuario = Usuario::findOrFail($email[0]->id_usu);


        $data = array('email' => $usuario->email,'nombre' => $usuario->nombre);

    Mail::to($usuario->email)->send(new EncuestaSolvex($data));
        return "success";
    }

and this is the template that is sent by email

@component('mail::message')
# Introduction

The body of your message.
@foreach ($data as $data)
<ul>
    Nombre: {{ $data }}
    </ul>
@endforeach
{{-- Nomre: {{ $data }}
 --}}
@component('mail::button', ['url' => ''])
Button Text
@endcomponent

Thanks,<br>
{{ config('app.name') }}
@endcomponent

When I want to take just one data, say name or email, it does not leave me, or rather I do not know how to do it, I can not put $data-nombre because it marks me that I try to invoke a non-object, so in what way could it?

    
asked by Dohko19 24.09.2018 в 17:22
source

1 answer

0

First of all, if you are going to go through an array, try not to assign the same variable name to the value or key of each iteration as the array you are going through.

foreach($data as $data)  //Esto esta mal

foreach($data as $item)  //Esto esta bien

Now if you see a arreglo in this way:

$data = array('email' => $usuario->email,'nombre' => $usuario->nombre);

You can access the in vista in this way:

Nombre: {{ $data['nombre'] }}
Correo: {{ $data['email'] }}

If you want to access the indexes as if it were an object you must pass the array as an instance of stdObject , that is, in your controladora you should replace

THIS :

$data = array('email' => $usuario->email,'nombre' => $usuario->nombre);

POR:

$data = (object) array('email' => $usuario->email,'nombre' => $usuario->nombre);

And already in your plantilla you can show it this way:

Nombre: {{ $data->nombre }}
Correo: {{ $data->email }}

If you do not want to show each element separately and reduce it in a single line you can use a cycle foreach and it would be:

{% foreach($data as $key => $item) %}
    {{ ucfirst($key) }}: {{ $item }}
{% endforeach %}

This way you would show something like

Nombre: nombre1
Correo: [email protected]
    
answered by 24.09.2018 / 17:40
source