1

I am perfoming a query in my controller and outputting the total cost on my view.

Heres my query:

 $stat = array(
            'TotalCost' => DB::table('ORD_DETAIL')
                        ->select(DB::raw('SUM(OD_QTYORD * OD_QTYUNIT * OD_COSTPRICE) as TotalCost'))
                        ->where('OD_ORDER_NUMBER',$id)->first()
            );

Heres my output:

{{ $stat['TotalCost'] }}

But I am getting the error:

htmlentities() expects parameter 1 to be string, object given (View: F:\view.blade.php)

1
  • Try adding get() after first() - ...first()->get() Commented Apr 28, 2016 at 15:39

2 Answers 2

2

The reason you're getting the above error is because you're query is returning an Object.

To get around this you can either do:

{{ $stat['TotalCost']->TotalCost }}

Or you could change your query to use Laravel's built-in sum() method:

DB::table('ORD_DETAIL')
    ->where('OD_ORDER_NUMBER',$id)
    ->sum(DB::raw('OD_QTYORD * OD_QTYUNIT * OD_COSTPRICE'));

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

0

Change it to

{{ $stat['TotalCost']->TotalCost }}

To maintain

{{ $stat['TotalCost'] }}

change your query to

 $stat = array(
        'TotalCost' => DB::table('ORD_DETAIL')
                    ->select(DB::raw('SUM(OD_QTYORD * OD_QTYUNIT * OD_COSTPRICE) as TotalCost'))
                    ->where('OD_ORDER_NUMBER',$id)->lists('TotalCost')->first();
        );

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.