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.)
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
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
php artisan make:mail Notify
than make order.blade.php in a emails folder it'll be a simple 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
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 = '[email protected]';
$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("[email protected]")->send(new Notify($order));
}
}