1

I am trying to make my sales app send a customer a message with all the purchased items with their prices. The purchased items are stored in the dp, each item with its quantity and price.

I have fetched all the purchased items, but I can't put the items' name and prices into a single sting - which is the message to be send to the customer.

Below is my items array object

$order = App\Order::find($id);
$items = $order->orderItems;

$item = OrderItem::where('order_id', $order->id)->first();

 $message = $order->code . " confirmed. ".$order->customer->name ." ". $order->customer->vehicle_plate . " has paid KES ".$order->amount."  for ".$item->quantity." ".$item->item->unit->name." of ".$item->item->name." at ".$order->updated_at->format('d/m/Y h:i a');

This message has the first item of the order. I need a single message with all order items.

2
  • You can iterate through all items and concatenated the generated text for each product to a variable, then display that variable. Let's say you have a function getItemOrderText which returns the message for a single item. You could then do something like $message .= getItemOrderText($currentItem) . '<br />'. Commented Feb 25, 2020 at 8:21
  • Works like magic. Thank you Commented Feb 25, 2020 at 8:31

2 Answers 2

1

You could do this:

$order = App\Order::find(14);
$items = $order->orderItems;

$itemMessage = "";
foreach($items as $item){
    $itemMessage .= $item->quantity." ".$item->item->unit->name." ".$item->item->name." ";
}

$message = $order->code . " confirmed. ".$order->customer->name ." ". $order->customer->vehicle_plate . " has paid KES ".$order->amount."  for ".$itemMessage." at ".$order->updated_at->format('d/m/Y h:i a');

return $message;
Sign up to request clarification or add additional context in comments.

Comments

0

you can concatenate item data by iterating through the loop

$order = App\Order::find($id);
$items = $order->orderItems;

$item = OrderItem::where('order_id', $order->id)->first();
$message = $order->code . " confirmed. ".$order->customer->name ." ".     $order->customer->vehicle_plate . " has paid KES ".$order->amount."  for ";

foreach($item as $item_value){
    $message .=  $item_value->quantity." ".$item_value->item->unit->name." of ".$item_value->item->name;
 }
$message .= " at ".$order->updated_at->format('d/m/Y h:i a');

echo $message; 

Comments

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.