Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

claudio88's avatar

Sending an associative arrays of associative arrays to email tamplete

Hello I am trying to figure out how to send an associative array of associative arrays to my mail template. So far I was able to send an associative array of order data ($data) to it but now I want to send order items associated with the order through an associative array of associative arrays.

This is my Controller

// inseram datele de livrare si cele legate de plata in tabelul orders
        // $orders_id preia id-ul comenzii (id din tabelul orders)
        $order_id = Order::insertGetId([
            'user_id' => Auth::id(),
            'division_id' => $request->division_id,
            'district_id' => $request->district_id,
            'shipping_first_name' => $request->shipping_first_name,
            'shipping_last_name' => $request->shipping_last_name,
            'shipping_phone' => $request->shipping_phone,
            'shipping_email' => $request->shipping_email,
            'shipping_street' => $request->shipping_street,
            'shipping_street_number' => $request->shipping_street_number,
            'shipping_building' => $request->shipping_building,
            'shipping_apartment' => $request->shipping_apartment,
            'notes' => $request->notes,

            'payment_type' => 'Stripe',
            'payment_method' => 'Stripe',
            'payment_type' => $charge->payment_method,
            'transaction_id' => $charge->balance_transaction,
            'currency' => $charge->currency,
            'amount' => $total_amount,
            'order_number' => $charge->metadata->order_id,

            'invoice_no' => 'UPT_' . $charge->metadata->order_id,
            'order_date' => Carbon::now()->format('d F Y'),
            'order_month' => Carbon::now()->format('F'),
            'order_year' => Carbon::now()->format('Y'),
            'status' => 'In procesare',
            'created_at' => Carbon::now(),
        ]);

        // $carts preia din cosul de cumparaturi toate produsele produsele
        $carts = Cart::content();
        // iteram cu $carts ca sa preluam fiecare produs din cosul de cumparaturi
        // si il inseram in tabelul order_items (produse comandate)
        foreach ($carts as $cart) {
            OrderItem::insert([
                // inseram in order_id id-ul comenzii inserate mai sus in tabelul orders
                // si restul datelor preluate din cosul de cumparaturi
                'order_id' => $order_id,
                'product_id' => $cart->id,
                'product_name' => $cart->name,
                // 'color' => $cart->options->color,
                // 'size' => $cart->options->size,
                'qty' => $cart->qty,
                'price' => $cart->price,
                'created_at' => Carbon::now(),
            ]);
        }

        // trasmite mailul cu datele comenzii dupa finalizare plata
        // $invoice preia din tabelul acea comanda care are id=$order_id
        $invoice = Order::findOrFail($order_id);
        // $data preia datele comenzii
        $data = [
            'order_number' => $invoice->order_number,
            'first_name' => $invoice->shipping_first_name,
            'last_name' => $invoice->shipping_last_name,
            'phone' => $invoice->shipping_phone,
            'email' => $invoice->shipping_email,
            'amount' => $total_amount,
            'street' => $invoice->shipping_street,
            'street_number' => $invoice->shipping_street_number,
            'building' => $invoice->shipping_building,
            'apartment' => $invoice->shipping_apartment,
            'payment_method' => $invoice->payment_method,
            'transaction_id' => $invoice->transaction_id,
            'amount' => $total_amount,
            'order_date' => $invoice->order_date,
        ];

        // $products preia din tabelul order_items produsele pentru care order_id=$order_id inserat mai sus
        // $products = OrderItem::where('order_id', $order_id)->get();

        // foreach ($products as $product=>$key) {
        //     $item['products'][] = [
        //         'name' => $product->product_name,
        //         'qty' => $product->qty,
        //         'price' => $product->price,
        //     ];
        // }

        // trimite spre mail-ul din request afereten adresei de livrare (user email) 
        // toate datele comenzii (data, numarul comenzii, totalul comenzii) prin custom mail creat -> app\Mail\OrderMail.php
        Mail::to($request->shipping_email)->send(new OrderMail($data));

        // dupa inserare comanda daca sesiunea are voucher stergem voucherul din sesiune
        if (Session::has('voucher')) {
            Session::forget('voucher');
        }

        // stergere cosul de cumparaturi dupa inserare comanda
        Cart::destroy();
        $notification = array(
            'message' => 'Comanda a fost inregistrata cu succes!',
            'alert-type' => 'success'
        );
        return redirect()->route('welcome')->with($notification);

This is my OrderMail.php

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class OrderMail extends Mailable
{
    use Queueable, SerializesModels;

    // aici primim datele StripeController
    public $data;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        // preluam in $order toate datele comenzii
        $order = $this->data;
        // returnam emailul cu datele comenzii de la adresa de email a utilizatorului
        // si returnam view-ul care contine datele comenzii
        return $this->from('[email protected]')->view('mail.order_mail', compact('order'))->subject('Comanda de la eShop UPT');
    }
}

So my questions are as follows.

  1. How do I write this so I can get an associative array of associative arrays of each product in the clients orders to be sent to the OrderMail.php and then to the email template?
   $products = OrderItem::where('order_id', $order_id)->get();

        foreach ($products as $product=>$key) {
            $item['products'][] = [
                'name' => $product->product_name,
                'qty' => $product->qty,
                'price' => $product->price,
            ];
        }
  1. In the email template do I also need to foreach the order item products of the associative arrays and how would I also write that?

  2. Is it also possible to send images of the products to the email tamplete if say I add a column in the order_items table with the image location?

0 likes
0 replies

Please or to participate in this conversation.