【问题标题】:How to convert array of values into strings in laravel?如何在laravel中将值数组转换为字符串?
【发布时间】:2021-09-24 01:38:12
【问题描述】:

我正在开发一个控制器,它负责发送包含书籍详细信息的邮件(这些详细信息我从数据库中获取),这些是作为对象数组出现的,但我需要在这里传递一个数据,例如一个普通的字符串,如何将这个对象数组转换为字符串,请帮助我如何实现这个东西......

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

【问题讨论】:

    标签: laravel laravel-8


    【解决方案1】:
    /**
     * 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)
    }
    

    【讨论】:

    • 感谢您的回答,是否可以在邮件中显示所有价格的总和
    • ErrorException: array_map(): Expected parameter 2 to be an array, null 在文件 C:\xampp\htdocs\laravel-bookstore\app\Notifications\orderSuccessfullNotification.php 中给出,我得到这种类型邮递员的错误
    • 我收到错误消息,它没有发送任何电子邮件@The One Above All
    • @Sravani 再次尝试。我已经更新了答案。
    • 非常感谢@The One,我还需要一个要求,你能帮帮我吗
    【解决方案2】:

    您正在将直接对象传递给 MailMessage,请将通知中的 toMail 方法更改为此。

    这应该设置 book1 的名称,但是可以简化整个类。

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

    【讨论】:

    • 感谢您的回答@frogeyedman,我想从数据库中获取值,并且我想打印所有不同于 [0] 的数组值
    猜你喜欢
    • 2021-09-02
    • 2019-09-13
    • 1970-01-01
    • 2015-11-11
    • 2021-12-05
    • 1970-01-01
    • 2020-06-14
    • 1970-01-01
    • 2021-12-13
    相关资源
    最近更新 更多