简体   繁体   English

使用laravel 5.3通知将集合传递到电子邮件视图

[英]Passing collection to email view using laravel 5.3 Notifications

I have created a notification class to send clients an order confirmation email. 我创建了一个通知类,用于向客户发送订单确认电子邮件。 In this I would like to add the details from their order which is a collection from eloquent. 在此,我想从他们的命令中添加细节,这是雄辩的集合。 I have the data in the notifications class. 我在通知类中有数据。 (If I dd($this->order) I get the expected result.) (如果我dd($ this-> order)我得到预期的结果。)

My Problem is passing the data from the class to the email blade template. 我的问题是将数据从类传递到电子邮件刀片模板。 I have read through the docs but nothing stands to me out that I can use to pass the whole collection and then to be able to format it in the view. 我已经阅读了文档,但是没有什么让我感到特别的是,我可以用来传递整个集合,然后在视图中对其进行格式化。

This is my OrderConfimation class 这是我的OrderConfimation

class OrderConfirmation extends Notification
{
    use Queueable;

    public $order;
    public $user;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($user , $order)
    {
        $this->order = $order;
        $this->user = $user;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->subject('Order Confirmation ['.$this->order->ID.']')
            ->greeting('Hi '.$this->user->F_NAME)
            ->line('Thank you for ordering from Awesome Store.')
            ->line('Once Payment has been confirmed we will send you a Payment Confirmation Email')
            ->line('As your order progresses we will send you updates. You can also track your order status by viewing it on our site at anytime')
            ->action('View Order', 'order url here')
            ->line('Thank you for shopping with us!');
           /**** What do i use here to pass $this->order to the view ????****/

    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }

I feel the answer will be so easy because it's laravel, I'm just missing it 我觉得答案很容易,因为它是幼虫,我只是想念它

use mailable. 使用可邮寄的。 Here's a basic sample. 这是一个基本示例。

first on cmd write 首先在cmd上写

php artisan make:mail Notify

than make order.blade.php in a emails folder it'll be a simple html 比在电子邮件文件夹中制作order.blade.php它将是一个简单的html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Notification</title>
</head>
<body>
<p>
Your order #{{$order['id']}}
</p>    
</body>
</html>

open notify in App\\Mail App \\ Mail中打开通知

namespace App\Mail;

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

class Notify extends Mailable
{
    use Queueable, SerializesModels;
    public $order;
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($order)
    {
        // to put order data from controller to mailable 
        $this->order=$order;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        $address = 'site@info.com';
        $name = 'site.com';
        $subject = 'notification';
        return $this->view('emails.order')->from($address, $name)
        ->subject($subject)
        ->with(['order'=>$this->order]);
        //emails its a folder
        //order its a order.blade.php file
    }
}

in any controller 在任何控制器中

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Mail\Notify;//load notify mailable
use Mail;// use mail class
class Demo extends Controller
{
    //
    public function save(Request $post){
        $order=array('id'=>'1','desc'=>'lorem ipsum dolore');
        Mail::to("some@mail.com")->send(new Notify($order));
    }
}

check mailtrap incoming 检查收到的邮件陷阱

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM