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

glena072's avatar

Object class Money could not be converted to string

Hi, All long-time subscriber first-time poster.

I am having an issue returning a formatted price for an order using Money. i have a controller for the order;

$order = new Order();

        $order->order_number = uniqid();
        $order->user_id = auth()->id();
        $order->vendor_id = $vendor_id;
        $order->order_total = Cart::total();

        $order->save();
        // $request->user()->orders()->create([$order]);

        $cart_items = Cart::content();

        foreach ($cart_items as $product) {
            $order->products()->attach($product->id, [
                'price' => $product->price,
                'quantity' => $product->qty,
                'milk' => $product->options->milk,
                'sugar' => $product->options->sugar,
                'syrup' => $product->options->syrup

            ]);
        }
        $numberStamps = $product->qty;
        for ($i = 0; $i < $numberStamps; $i++) {

            $stamp = new Stamp;
            $stamp->card_id = $new_card->id;
            $stamp->order_id = $order->id;
            $stamp->user_id = auth()->id();
            $stamp->vendor_id = $vendor_id;
            $stamp->order_id = $order->id;

            $stamp->save();
        }
    }


    $confirm_url = URL::signedRoute('confirm_order.update', $order->id);

    // email customer and vendor
    // Mail::to($order->vendor->email)->send(new orderSubmitted($order));
    Mail::to('[email protected]')
    ->send(new orderSubmitted($order, $confirm_url));

    // empty cart
    Cart::destroy();

    // redirect to order submitted page;
    return redirect()->route('order.submitted', $order);

in my model I have a method;

public function getFormattedPrice() { $price = Money::AUD($this->order_total); return (string) $price; }

I call the function in my view;

@extends('layouts.app') @section('content')

{{-- # --}} {{-- Venue --}} Date Image Product Total Status @auth
          @if($order->count() > 0)
          <tr>
            {{-- <th scope="row">1</th> --}}
            {{-- <td>{{$order->vendor_id}}</td> --}}
            <td>{{$order->getFormattedDate('date')}}</td>
            @foreach($order->products as $product) {{--{{asset('/storage/app/public/img/'.'$product->product_image')}}--}}
            <td><img src="/storage/img/{{$product->product_image}}" width="48" height="48"></td>
            <td>{{$product->productName}}<div><small class="text-muted">Milk - {{$product->pivot->milk}}, Sugar - {{$product->pivot->sugar}}, Syrup - {{$product->pivot->syrup}}</small></div></td>
            <td>${{$order->getFormattedPrice('order_total')}}</td>
            <td><a href="{{ route('confirm_order.update', $order) }}" class="btn btn-success btn-sm"><i class="fas fa-check-square"></i></a></td>
            @endForEach
          </tr>
          @else
            <p>It's strange, you dont any orders yet!</P>
          @endif
          @endauth
        </tbody>
      </table>
</div><!-- /.row -->
@endsection

i get the following error

Error Object of class Money\Money could not be converted to string (View: C:\xampp\htdocs\nudecode_cpanel\resources\views\orders_vendor.blade.php)

any advice would be greatly appreciated

.

0 likes
5 replies
mohammadelkoumi's avatar
public function getFormattedPrice() :  string 
{ 
       return money_format('%i', $this->order_total);
}
2 likes

Please or to participate in this conversation.