Code cracker
Code cracker

Reputation: 396

How to convert array of values into strings in laravel?

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

Upvotes: 1

Views: 573

Answers (2)

Ramanath
Ramanath

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

frogeyedman
frogeyedman

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

Related Questions