1

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

This is how i am getting

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 [
            //
        ];
    }
}

2 Answers 2

2
/**
 * 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)
}
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for your answer, is this possible to display sum of all prices in mail message
ErrorException: array_map(): Expected parameter 2 to be an array, null given in file C:\xampp\htdocs\laravel-bookstore\app\Notifications\orderSuccessfullNotification.php, i am getting this type of error in postman
i am getting error it's not sending any email @The One Above All
@Sravani try agin. I've updated the answer.
Thank you so much @The One Above All, i need one more requirement can u please help me out
|
0

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

1 Comment

Thanks for your answer @frogeyedman, i want to fetch the values from database and i want to print all the array values not like [0]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.