\n
CustomersController.php
public function orderSuccessfull(Request $request){\n $cust=new Customers();\n $cust->user_id = auth()->id();\n $cust_id=Customers::where('user_id',$cust->user_id)->value('user_id');\n $user_email=User::where('id',$cust_id)->value('email'); \n $order = User::where('email', $user_email)->first();\n $ord = Orders::create( \n [\n 'orderNumber' => $order->orderNumber=Str::random(6),\n 'customer_id'=>$order->id,\n 'order_date'=>$order->order_date=Carbon::now(), \n ]\n );\n \n $bookgetter1 = DB::table("Books")->select('name')->where('cart',['1'])->get();\n $bookgetter2 = DB::table("Books")->select('price')->where('cart',['1'])->get();\n $bookgetter3 = DB::table("Books")->select('author')->where('cart',['1'])->get();\n \n if($order && $ord){\n $order->notify(new orderSuccessfullNotification($ord- \n >orderNumber,$bookgetter1,$bookgetter2,$bookgetter3));\n }\n\n return response()->json(['message'=>'order created successfully']);\n }\n
\norderSuccessfullNotification.php
<?php\n\nnamespace App\\Notifications;\n\nuse Illuminate\\Bus\\Queueable;\nuse Illuminate\\Contracts\\Queue\\ShouldQueue;\nuse Illuminate\\Notifications\\Messages\\MailMessage;\nuse Illuminate\\Notifications\\Notification;\n\nclass orderSuccessfullNotification extends Notification\n{\n use Queueable;\n public $orderNumber;\n public $bookgetter1;\n public $bookgetter2;\n public $bookgetter3;\n\n /**\n * Create a new notification instance.\n *\n * @return void\n */\n public function __construct($orderNumber,$bookgetter1,$bookgetter2,$bookgetter3)\n {\n\n $this->orderNumber = $orderNumber;\n $this->bookgetter1=$bookgetter1;\n $this->bookgetter2=$bookgetter2;\n $this->bookgetter3=$bookgetter3;\n }\n\n /**\n * Get the notification's delivery channels.\n *\n * @param mixed $notifiable\n * @return array\n */\n public function via($notifiable)\n {\n return ['mail'];\n }\n\n /**\n * Get the mail representation of the notification.\n *\n * @param mixed $notifiable\n * @return \\Illuminate\\Notifications\\Messages\\MailMessage\n */\n public function toMail($notifiable)\n {\n return (new MailMessage)\n ->line("You'r order has been placed successfully.. ")\n ->line('This is the order-id keep it furthur!')\n ->with($this->orderNumber)\n ->with($this->bookgetter1)\n ->with($this->bookgetter2)\n ->with($this->bookgetter3);\n }\n\n /**\n * Get the array representation of the notification.\n *\n * @param mixed $notifiable\n * @return array\n */\n public function toArray($notifiable)\n {\n return [\n //\n ];\n }\n}\n\n
\n","author":{"@type":"Person","name":"Code cracker"},"upvoteCount":1,"answerCount":2,"acceptedAnswer":{"@type":"Answer","text":"/**\n * Get the mail representation of the notification.\n *\n * @param mixed $notifiable\n * @return \\Illuminate\\Notifications\\Messages\\MailMessage\n */\npublic function toMail($notifiable)\n{\n $bookgetterPrices = json_decode($this->bookgetter2);\n $totalPrice = 0;\n foreach ($bookgetterPrices as $p) {\n $totalPrice += $p->price;\n }\n\n $bookgetter1 = implode(',', array_map(function($x) { return $x->name; }, json_decode($this->bookgetter1)));\n $bookgetter2 = implode(',', array_map(function($x) { return $x->price; }, $bookgetterPrices));\n $bookgetter3 = implode(',', array_map(function($x) { return $x->author; }, json_decode($this->bookgetter3)));\n \n return (new MailMessage)\n ->line("You'r order has been placed successfully.. ")\n ->line('This is the order-id keep it furthur!')\n ->with($this->orderNumber)\n ->with($totalPrice)\n ->with($bookgetter1)\n ->with($bookgetter2)\n ->with($bookgetter3)\n}\n
\n","author":{"@type":"Person","name":"Ramanath"},"upvoteCount":2}}}Reputation: 396
I am developing one controller which is responsible for send a mail with the books details(these details i am fetching from database),these are coming as a array of objects but what i need here is i want to pass a data like a normal strings, How to convert this array of objects to the strings ,please help me how to acheive this thing...
CustomersController.php
public function orderSuccessfull(Request $request){
$cust=new Customers();
$cust->user_id = auth()->id();
$cust_id=Customers::where('user_id',$cust->user_id)->value('user_id');
$user_email=User::where('id',$cust_id)->value('email');
$order = User::where('email', $user_email)->first();
$ord = Orders::create(
[
'orderNumber' => $order->orderNumber=Str::random(6),
'customer_id'=>$order->id,
'order_date'=>$order->order_date=Carbon::now(),
]
);
$bookgetter1 = DB::table("Books")->select('name')->where('cart',['1'])->get();
$bookgetter2 = DB::table("Books")->select('price')->where('cart',['1'])->get();
$bookgetter3 = DB::table("Books")->select('author')->where('cart',['1'])->get();
if($order && $ord){
$order->notify(new orderSuccessfullNotification($ord-
>orderNumber,$bookgetter1,$bookgetter2,$bookgetter3));
}
return response()->json(['message'=>'order created successfully']);
}
orderSuccessfullNotification.php
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class orderSuccessfullNotification extends Notification
{
use Queueable;
public $orderNumber;
public $bookgetter1;
public $bookgetter2;
public $bookgetter3;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($orderNumber,$bookgetter1,$bookgetter2,$bookgetter3)
{
$this->orderNumber = $orderNumber;
$this->bookgetter1=$bookgetter1;
$this->bookgetter2=$bookgetter2;
$this->bookgetter3=$bookgetter3;
}
/**
* 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)
->line("You'r order has been placed successfully.. ")
->line('This is the order-id keep it furthur!')
->with($this->orderNumber)
->with($this->bookgetter1)
->with($this->bookgetter2)
->with($this->bookgetter3);
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
Upvotes: 1
Views: 573
Reputation: 530
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
$bookgetterPrices = json_decode($this->bookgetter2);
$totalPrice = 0;
foreach ($bookgetterPrices as $p) {
$totalPrice += $p->price;
}
$bookgetter1 = implode(',', array_map(function($x) { return $x->name; }, json_decode($this->bookgetter1)));
$bookgetter2 = implode(',', array_map(function($x) { return $x->price; }, $bookgetterPrices));
$bookgetter3 = implode(',', array_map(function($x) { return $x->author; }, json_decode($this->bookgetter3)));
return (new MailMessage)
->line("You'r order has been placed successfully.. ")
->line('This is the order-id keep it furthur!')
->with($this->orderNumber)
->with($totalPrice)
->with($bookgetter1)
->with($bookgetter2)
->with($bookgetter3)
}
Upvotes: 2
Reputation: 534
You are passing the direct objects to the MailMessage, change the toMail method in your notification to this.
This should set the name of book1, however this whole class can be simplified.
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->line("You'r order has been placed successfully.. ")
->line('This is the order-id keep it furthur!')
->line($this->bookgetter1[0]->name)
->with($this->orderNumber)
}
Upvotes: 0