【问题标题】:Passing collection to email view using laravel 5.3 Notifications使用 laravel 5.3 通知将集合传递给电子邮件视图
【发布时间】:2017-06-19 17:40:08
【问题描述】:

我创建了一个通知类来向客户发送订单确认电子邮件。在此,我想从他们的订单中添加详细信息,这是来自 eloquent 的集合。我在通知类中有数据。 (如果我 dd($this->order) 我得到了预期的结果。)

我的问题是将数据从类传递到电子邮件刀片模板。我已经通读了文档,但没有任何东西可以让我用来传递整个集合,然后能够在视图中对其进行格式化。

这是我的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 [
            //
        ];
    }

我觉得答案会很简单,因为它是 laravel,我只是想念它

【问题讨论】:

  • 只是出于好奇,您为什么使用Notification 类来发送电子邮件。为什么不创建一个Mailable 类? laravel.com/docs/5.3/mail#writing-mailables
  • 在阅读了关于通知的信息后,这对我来说很有意义。我对 laravel 还是很陌生,所以我不知道我可以使用 Mailable。那我可能误解了通知的使用吗?

标签: php laravel email laravel-5.3


【解决方案1】:

使用可邮寄。这是一个基本示例。

先在 cmd 上写入

php artisan make:mail Notify

emails 文件夹中创建 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>

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
    }
}

在任何控制器中

<?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

【讨论】:

  • 这就是我所做的。关于使用通知中的刀片模板有点破解它,但它可以工作,所以我所有的邮件看起来和感觉都一样。
猜你喜欢
  • 2017-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-06
  • 2017-10-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多