Home > Blockchain >  Pass different data on a Laravel Mailable
Pass different data on a Laravel Mailable

Time:04-03

I need to send confirmation emails on a checkout page. But with the current way I only pass $checkout.

This is the database structure of what data I need to pass: database structure

this is how the $checkout gets passed to the Mailable

// send confirmation email to customer
Mail::to($request->email)->send(new CheckoutConfirmation($checkout));

Mailable:

<?php

namespace App\Mail;

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

use App\Models\Events;


class CheckoutConfirmation extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($checkout)
    {
        $this->checkout = $checkout;
        // get the course data from the database
        $event = Events::query()
            ->where('id', '=', $this->checkout->event_id)
            ->first();

        // this needs to be passed, along $checkout
        // $event->title;
        // $event->start;
        // $event->end;


    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('emails.checkout.customer')->subject('Wir freuen uns schon auf Sie! Hier ist die Bestätigung Ihrer Buchung.')->with('checkout', $this->checkout);
    }
}

the issue with how it is currently handeled is that I can only call:

$checkout->xxx

but I also need to call $event->xxx in the email blade.

CodePudding user response:

Look at this example

/**
     * The user details.
     *
     * @var $details
     */
    public $details;

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

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->to(data_get($this->details,'email'), env('APP_NAME'))
            ->replyTo(config('mail.from.address'), ucwords(data_get($this->details, 'name')))
            ->subject(data_get($this->details, 'subject'))
            ->view('emails.contact_us');
    }

You can use $details; then use data_get you can put $this->details then get the key whatever email or anything else .

In Controller when called Class Mail in this way

    Mail::send(new ContactUsMail($request->validated()));

CodePudding user response:

Based on this StackOverflow answer, I changed my Mailable to the following:

<?php

namespace App\Mail;

use App\Models\Events;
use App\Models\Checkout;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class CheckoutConfirmation extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($checkout)
    {
        $this->checkout = Checkout::query()->where('event_id', '=', $checkout->event_id)->first();;
        $this->event = Events::query()->where('id', '=', $this->checkout->event_id)->first();

    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('emails.checkout.customer')->subject('Wir freuen uns schon auf Sie! Hier ist die Bestätigung Ihrer Buchung.')->with(['checkout'=>$this->checkout, 'event'=>$this->event]);
    }
}

now I can pass arrays to the template: ->with(['checkout'=>$this->checkout, 'event'=>$this->event])

this allows to add as much data as needed.

  • Related