jeudi 27 avril 2017

Laravel 5.4 - Send mail with customer AND product-data

I have some problems with passing some data to the email view.

http://ift.tt/2o4SyIX

Thats the Mail functions I'm using. I'm completely new to sendings mails automated with php/laravel.

I've created a Laravel Email Class with

php artisan make:mail OrderCompleted --markdown=blabla/orderCompleted

After this command I've got this class now:

<?php

namespace App\Mail;

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

use App\Models\Customers;  <-- Customer Model

class OrderCompleted extends Mailable
{
    use Queueable, SerializesModels;

    public $customer;

    public function __construct(Customers $customer)
    {
        $this->customer = $customer;
    }

    public function build()
    {
        return $this->markdown('emails.orderCompleted');
    }
}

And in my view:

@component('mail::panel', ['url' => ''])
<label>Name</label>
<span> </span>
<br>
<label>Email</label>
<span>  </span>
<br>
<label>Adress</label>
<span>,  </span>
<br>
@endcomponent

@component('mail::button', ['url' => ''])
    bla bla bla 
@endcomponent

Thanks,<br>

@endcomponent

Thats the Ajax-Controller with the code how I send the email:

 $customer = new Customers;
        $customer->email = $request['email'];
        $customer->first_name = $request['first_name'];
        $customer->last_name = $request['last_name'];
        $customer->street = $request['street'];
        $customer->zip = $request['zip'];
        $customer->city = $request['city'];
        $customer->save();

foreach ($request->session()->get('products') as $product) {
            $customer->products()->attach($product->id);
        }

\Mail::to($customer)->send(new OrderCompleted($customer));

My Problem now is that I also want the data of the products the customer bought.

I would get them with this code below:

$customerData = Customers::where( bla bla equal to bla bla)->with('products')->get();

But I can't get those data to the html mail.blade - only the $customer data- but I also want to get the products he bought.

I've tried something like this in the OrderCompleted ( Mail Class )

 public function build(Customers $customer)
    {
    $customerData = Customers::where( bla bla equal to bla bla)->with('products')->get();
    return $this->markdown('emails.orderCompleted')->with('customerData' => $customerData);
    }

But this haven't worked for me. Does someone know's a solution for that?

I'm sorry for my bad english. Thanks for your help!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire